<!DOCTYPE html>
<html lang="en-US" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>静态资源压缩 | VitePress</title>
    <meta name="description" content="A VitePress site">
    <link rel="preload stylesheet" href="/notebook/assets/style.3dbfd0c2.css" as="style">
    
    <script type="module" src="/notebook/assets/app.8aaa4cbe.js"></script>
    <link rel="preload" href="/notebook/assets/inter-roman-latin.2ed14f66.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/notebook/assets/chunks/framework.1336c4e5.js">
    <link rel="modulepreload" href="/notebook/assets/chunks/theme.20cddc0c.js">
    <link rel="modulepreload" href="/notebook/assets/Nginx_基础篇.md.c7d8bb50.lean.js">
    <script id="check-dark-light">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-255ec12d><!--[--><!--]--><!--[--><span tabindex="-1" data-v-ae3e3f51></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-ae3e3f51> Skip to content </a><!--]--><!----><header class="VPNav" data-v-255ec12d data-v-7e5bc4a5><div class="VPNavBar has-sidebar" data-v-7e5bc4a5 data-v-0937f67c><div class="container" data-v-0937f67c><div class="title" data-v-0937f67c><div class="VPNavBarTitle has-sidebar" data-v-0937f67c data-v-86d1bed8><a class="title" href="/notebook/" data-v-86d1bed8><!--[--><!--]--><!--[--><img class="VPImage logo" src="/notebook/Vue.png" alt data-v-8426fc1a><!--]--><!--[-->任硕的文档<!--]--><!--[--><!--]--></a></div></div><div class="content" data-v-0937f67c><div class="curtain" data-v-0937f67c></div><div class="content-body" data-v-0937f67c><!--[--><!--]--><div class="VPNavBarSearch search" style="--vp-meta-key:&#39;Meta&#39;;" data-v-0937f67c><!--[--><!----><div id="docsearch"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg class="DocSearch-Search-Icon" width="20" height="20" viewBox="0 0 20 20" aria-label="search icon"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-0937f67c data-v-7f418b0f><span id="main-nav-aria-label" class="visually-hidden" data-v-7f418b0f>Main Navigation</span><!--[--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>Java学前端</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/HTML+JS.html" data-v-2f2cfafc><!--[-->HTML+JS<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/CSS.html" data-v-2f2cfafc><!--[-->CSS<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/Vue2+%E7%BB%84%E4%BB%B6.html" data-v-2f2cfafc><!--[-->Vue2+组件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/Vue3+%E7%BB%84%E4%BB%B6.html" data-v-2f2cfafc><!--[-->Vue3+组件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/React.html" data-v-2f2cfafc><!--[-->React<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>软件测试</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80.html" data-v-2f2cfafc><!--[-->测试基础<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E5%8E%8B%E5%8A%9B%E6%B5%8B%E8%AF%95.html" data-v-2f2cfafc><!--[-->压力测试<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>多线程</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E5%B9%B6%E5%8F%91%20&amp;%20%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-2f2cfafc><!--[-->基础篇<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E5%B9%B6%E5%8F%91%20&amp;%20%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%B9%B6%E5%8F%91%E5%AE%8C%E5%96%84.html" data-v-2f2cfafc><!--[-->进阶篇<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>开发工具</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/Chrome.html" data-v-2f2cfafc><!--[-->Chrome<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/IDEA%E5%9F%BA%E7%A1%80.html" data-v-2f2cfafc><!--[-->IDEA基础<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/IDEA%E6%8F%92%E4%BB%B6.html" data-v-2f2cfafc><!--[-->IDEA插件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/VS%20Code.html" data-v-2f2cfafc><!--[-->VS Code<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>消息中间件</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/RabbitMQ.html" data-v-2f2cfafc><!--[-->RabbitMQ<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/RocketMQ.html" data-v-2f2cfafc><!--[-->RocketMQ<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/Kafka.html" data-v-2f2cfafc><!--[-->Kafka<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/Canal.html" data-v-2f2cfafc><!--[-->Canal<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-0937f67c data-v-f6a63727><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-f6a63727 data-v-82b282f1 data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-82b282f1><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-82b282f1><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-0937f67c data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/renshuo123/renshuo123.github.io" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="#" aria-label="twitter" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Twitter</title><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg></a><a class="VPSocialLink no-icon" href="https://github.com/" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg t="1676028692954" class="icon" ...</path></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-0937f67c data-v-40855f84 data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-a7b5672a><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="icon" data-v-a7b5672a><circle cx="12" cy="12" r="2"></circle><circle cx="19" cy="12" r="2"></circle><circle cx="5" cy="12" r="2"></circle></svg></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><!----><!--[--><!--[--><!----><div class="group" data-v-40855f84><div class="item appearance" data-v-40855f84><p class="label" data-v-40855f84>Appearance</p><div class="appearance-action" data-v-40855f84><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-40855f84 data-v-82b282f1 data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-82b282f1><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-82b282f1><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div></div></div><div class="group" data-v-40855f84><div class="item social-links" data-v-40855f84><div class="VPSocialLinks social-links-list" data-v-40855f84 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/renshuo123/renshuo123.github.io" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="#" aria-label="twitter" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Twitter</title><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg></a><a class="VPSocialLink no-icon" href="https://github.com/" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg t="1676028692954" class="icon" ...</path></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-0937f67c data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><!----></header><div class="VPLocalNav reached-top" data-v-255ec12d data-v-5cfd5582><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-5cfd5582><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="menu-icon" data-v-5cfd5582><path d="M17,11H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,11,17,11z"></path><path d="M21,7H3C2.4,7,2,6.6,2,6s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,7,21,7z"></path><path d="M21,15H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,15,21,15z"></path><path d="M17,19H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,19,17,19z"></path></svg><span class="menu-text" data-v-5cfd5582>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-5cfd5582 data-v-18201f51><button data-v-18201f51>Return to top</button><!----></div></div><aside class="VPSidebar" data-v-255ec12d data-v-845b8fc6><div class="curtain" data-v-845b8fc6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-845b8fc6><span class="visually-hidden" id="sidebar-aria-label" data-v-845b8fc6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Java</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E6%96%B0%E7%89%B9%E6%80%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E9%9B%86%E5%90%88.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java集合</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java高级</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Linux</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Linux%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Linux基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Linux%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Linux新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Shell.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Shell脚本</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/%E5%AE%9E%E7%94%A8%E8%84%9A%E6%9C%AC.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>实用脚本</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/%E8%BD%AF%E4%BB%B6%E9%83%A8%E7%BD%B2.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>软件部署</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible has-active" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Nginx</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E5%AE%9E%E6%88%98%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>实战篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E9%9D%A2%E8%AF%95%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>面试篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SSM</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/Maven.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Maven</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/Spring.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Spring</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/SpringMVC.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringMVC</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/SpringBatch.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringBatch</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringBoot</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E5%BA%94%E7%94%A8%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>应用篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E6%96%B0%E7%89%B9%E6%80%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E8%BF%90%E7%BB%B4&amp;%E5%8E%9F%E7%90%86.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>运维&原理</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringCloud</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringCloud</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BF%85%E5%A4%87/Sentinel.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Sentinel</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringSecurity</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E9%AB%98%E7%BA%A7%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity高级篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Mybatis & MybatisPlus</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/Mybatis.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Mybatis</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/MybatisPlus.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MybatisPlus</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/JPA.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JPA</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Git & ChatGPT</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Git.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Git</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Github.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Github</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/ChatGPT.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ChatGPT</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Jenkins.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Jenkins</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Netty.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Netty</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>数据库</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>MySQL</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%AE%BE%E8%AE%A1.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL设计</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%90%E7%BB%B4.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL运维</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分库分表</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>Redis</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%8E%9F%E7%90%86.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis原理</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis高级</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%AE%9E%E6%88%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis实战</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/%E6%9C%AC%E5%9C%B0%E7%BC%93%E5%AD%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>本地缓存</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>MongoDB</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MongoDB/%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MongoDB基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MongoDB/%E6%95%B4%E5%90%88.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MongoDB进阶</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>ElasticSearch</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch/1%E3%80%81ES%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch/3%E3%80%81ES%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES高级</p><!--]--></a><!----></div><!----></div><!--]--></div></section><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/influxdb.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>InfluxDB</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Neo4j.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Neo4j</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>高并发 & 秒杀 & 分布式</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E5%88%86%E5%B8%83%E5%BC%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分布式理论</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BF%85%E5%A4%87/%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分布式锁</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E7%A7%92%E6%9D%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>秒杀</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E9%AB%98%E5%8F%AF%E7%94%A8.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高可用</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E9%AB%98%E5%B9%B6%E5%8F%91.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高并发</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>云原生</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%BA%91%E5%8E%9F%E7%94%9F/Docker.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Docker</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%BA%91%E5%8E%9F%E7%94%9F/K8S.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>K8S</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>可视化 & 监控</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E7%9B%91%E6%8E%A7%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>监控基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E7%9B%91%E6%8E%A7%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>监控进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%A7%E5%B1%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>可视化大屏</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/Zabbix.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Zabbix</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>学前端</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>HTML+CSS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/HTML%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>HTML基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/CSS%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>CSS基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/%E7%BD%91%E9%A1%B5%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>网页进阶</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>JS+TS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/JS%20%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JS基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/JS%20%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JS进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/ES6%20%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES6基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/ES6%20%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES6进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/TypeScript.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>TS基础</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>NodeJS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Node基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Node进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>Vue</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3高级</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E6%96%B0%E8%AF%AD%E6%B3%95.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3新语法</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue2/Vue2%E9%A1%B9%E7%9B%AE.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>小程序</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>小程序基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>小程序优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/uniapp.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>uniapp</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%A1%B9%E7%9B%AE.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>计算机基础</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>数据结构</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>操作系统</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>设计模式</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/%E7%BD%91%E7%BB%9C%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>计算机网络</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/UML.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>UML</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E7%AE%97%E6%B3%95/LeetCode.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>LeetCode</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>项目实战</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>云尚办公</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E4%BA%91%E5%B0%9A%E5%8A%9E%E5%85%AC/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>小兔鲜</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E8%BF%9B%E9%98%B6%E7%AF%871.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇1</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E8%BF%9B%E9%98%B6%E7%AF%872.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇2</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>地图</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>苍穹外卖</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E8%8B%8D%E7%A9%B9%E5%A4%96%E5%8D%96/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>黑马头条</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E8%BF%9B%E9%98%B6%E7%AF%872.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇2</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E9%AB%98%E7%BA%A7%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高级篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E6%94%AF%E4%BB%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>支付</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%A1%B9%E7%9B%AE%E6%8E%A8%E8%8D%90.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目推荐</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0" data-v-845b8fc6 data-v-9b797284><!----><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/team.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>团队成员</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-255ec12d data-v-669faec9><div class="VPDoc has-sidebar has-aside" data-v-669faec9 data-v-6b87e69f><!--[--><!--]--><div class="container" data-v-6b87e69f><div class="aside" data-v-6b87e69f><div class="aside-curtain" data-v-6b87e69f></div><div class="aside-container" data-v-6b87e69f><div class="aside-content" data-v-6b87e69f><div class="VPDocAside" data-v-6b87e69f data-v-3f215769><!--[--><!--]--><!--[--><!--]--><div class="VPDocAsideOutline" data-v-3f215769 data-v-ff0f39c8><div class="content" data-v-ff0f39c8><div class="outline-marker" data-v-ff0f39c8></div><div class="outline-title" data-v-ff0f39c8>On this page</div><nav aria-labelledby="doc-outline-aria-label" data-v-ff0f39c8><span class="visually-hidden" id="doc-outline-aria-label" data-v-ff0f39c8> Table of Contents for current page </span><ul class="root" data-v-ff0f39c8 data-v-d0ee3533><!--[--><!--]--></ul></nav></div></div><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-6b87e69f><div class="content-container" data-v-6b87e69f><!--[--><!--]--><!----><main class="main" data-v-6b87e69f><div style="position:relative;" class="vp-doc _notebook_Nginx_%E5%9F%BA%E7%A1%80%E7%AF%87" data-v-6b87e69f><div><h1 id="静态资源压缩" tabindex="-1">静态资源压缩 <a class="header-anchor" href="#静态资源压缩" aria-label="Permalink to &quot;静态资源压缩&quot;">​</a></h1><h2 id="为什么用-gzip" tabindex="-1">为什么用 Gzip <a class="header-anchor" href="#为什么用-gzip" aria-label="Permalink to &quot;为什么用 Gzip&quot;">​</a></h2><blockquote><p>建立在<strong>动静分离的基础之上</strong>，如果一个静态资源的Size越小，那么自然传输速度会更快，同时也会更节省带宽，因此我们在部署项目时，也可以通过Nginx对于静态资源实现压缩传输，一方面可以节省带宽资源，第二方面也可以加快响应速度并提升系统整体吞吐。</p></blockquote><blockquote><p>在Nginx也提供了三个支持资源压缩的模块ngx_http_gzip_module、ngx_http_gzip_static_module、ngx_http_gunzip_module，其中ngx_http_gzip_module属于内置模块，代表着可以直接使用该模块下的一些压缩指令，后续的资源压缩操作都基于该模块，先来看看压缩配置的一些参数/指令：</p></blockquote><h2 id="gzip-配置项" tabindex="-1">Gzip 配置项 <a class="header-anchor" href="#gzip-配置项" aria-label="Permalink to &quot;Gzip 配置项&quot;">​</a></h2><table><thead><tr><th style="text-align:left;">参数项</th><th style="text-align:left;">释义</th><th style="text-align:left;">参数值</th></tr></thead><tbody><tr><td style="text-align:left;">gzip</td><td style="text-align:left;">开启或关闭压缩机制</td><td style="text-align:left;">on/off;</td></tr><tr><td style="text-align:left;">gzip_types</td><td style="text-align:left;">根据文件类型选择性开启压缩机制</td><td style="text-align:left;">image/png、text/css...</td></tr><tr><td style="text-align:left;">gzip_comp_level</td><td style="text-align:left;">用于设置压缩级别，级别越高越耗时</td><td style="text-align:left;">1~9（越高压缩效果越好）</td></tr><tr><td style="text-align:left;">gzip_vary</td><td style="text-align:left;">设置是否携带Vary:Accept-Encoding头域的响应头部</td><td style="text-align:left;">on/off;</td></tr><tr><td style="text-align:left;">gzip_buffers</td><td style="text-align:left;">设置处理压缩请求的缓冲区数量和大小</td><td style="text-align:left;">数量 大小，如16 8k;</td></tr><tr><td style="text-align:left;">gzip_disable</td><td style="text-align:left;">针对不同客户端的请求来设置是否开启压缩</td><td style="text-align:left;">如 .Chrome.*;</td></tr><tr><td style="text-align:left;">gzip_http_version</td><td style="text-align:left;">指定压缩响应所需要的最低HTTP请求版本</td><td style="text-align:left;">如1.1;</td></tr><tr><td style="text-align:left;">gzip_min_length</td><td style="text-align:left;">设置触发压缩的文件最低大小</td><td style="text-align:left;">如512k;</td></tr><tr><td style="text-align:left;">gzip_proxied</td><td style="text-align:left;">对于后端服务器的响应结果是否开启压缩</td><td style="text-align:left;">off、expired、no-cache...</td></tr></tbody></table><p>在上述的压缩配置中，最后一个<code>gzip_proxied</code>选项，可以根据系统的实际情况决定，总共存在多种选项：</p><ul><li><code>off</code>：关闭<code>Nginx</code>对后台服务器的响应结果进行压缩。</li><li><code>expired</code>：如果响应头中包含<code>Expires</code>信息，则开启压缩。</li><li><code>no-cache</code>：如果响应头中包含<code>Cache-Control:no-cache</code>信息，则开启压缩。</li><li><code>no-store</code>：如果响应头中包含<code>Cache-Control:no-store</code>信息，则开启压缩。</li><li><code>private</code>：如果响应头中包含<code>Cache-Control:private</code>信息，则开启压缩。</li><li><code>no_last_modified</code>：如果响应头中不包含<code>Last-Modified</code>信息，则开启压缩。</li><li><code>no_etag</code>：如果响应头中不包含<code>ETag</code>信息，则开启压缩。</li><li><code>auth</code>：如果响应头中包含<code>Authorization</code>信息，则开启压缩。</li><li><code>any</code>：无条件对后端的响应结果开启压缩机制。</li></ul><h2 id="gzip-实战" tabindex="-1">Gzip 实战 <a class="header-anchor" href="#gzip-实战" aria-label="Permalink to &quot;Gzip 实战&quot;">​</a></h2><blockquote><p>在html目录下放置一个jquery.js，无需修改nginx.conf，因为本来根目录就是html</p></blockquote><h3 id="原生访问" tabindex="-1">原生访问 <a class="header-anchor" href="#原生访问" aria-label="Permalink to &quot;原生访问&quot;">​</a></h3><p>访问测试：<a href="http://192.168.22.146/jquery.js" target="_blank" rel="noreferrer">http://192.168.22.146/jquery.js</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301161203023.png" alt="image-20230116120309894" style="zoom:67%;"><h3 id="添加gizp配置访问" tabindex="-1">添加Gizp配置访问 <a class="header-anchor" href="#添加gizp配置访问" aria-label="Permalink to &quot;添加Gizp配置访问&quot;">​</a></h3><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">http</span><span style="color:#A6ACCD;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 开启压缩机制</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">gzip</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">on</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 指定会被压缩的文件类型(也可自己配置其他类型),不建议设置成*</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">gzip_types</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">text/plain</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">application/javascript</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">text/css</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">application/xml</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">text/javascript</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#FFCB6B;">image/jpeg</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">image/gif</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">image/png</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 设置压缩级别，越高资源消耗越大，但压缩效果越好</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">gzip_comp_level</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">5</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 在头部中添加Vary: Accept-Encoding（建议开启）</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">gzip_vary</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">on</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 处理压缩请求的缓冲区数量和大小</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">gzip_buffers</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">16</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">8</span><span style="color:#C3E88D;">k</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 对于不支持压缩功能的客户端请求不开启压缩机制</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">gzip_disable</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">MSIE [1-6]\.</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;"># 低版本的IE浏览器不支持压缩</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 设置压缩响应所支持的HTTP最低版本</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">gzip_http_version</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1.1</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 设置触发压缩的最小阈值</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">gzip_min_length</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#C3E88D;">k</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 关闭对后端服务器的响应结果进行压缩</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">gzip_proxied</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">off</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301161208402.png" alt="image-20230116120831263" style="zoom:67%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301161210377.png" alt="image-20230116121042250" style="zoom:67%;"><h3 id="对比区别" tabindex="-1">对比区别 <a class="header-anchor" href="#对比区别" aria-label="Permalink to &quot;对比区别&quot;">​</a></h3><blockquote><p>从图中可以很明显看出，未开启压缩机制前访问时，<code>js</code>文件的原始大小为<code>300K</code>，当配置好压缩后再重启<code>Nginx</code>，会发现文件大小从<code>300KB→85.7KB</code>，效果立竿见影！</p></blockquote><h2 id="注意事项" tabindex="-1">注意事项 <a class="header-anchor" href="#注意事项" aria-label="Permalink to &quot;注意事项&quot;">​</a></h2><blockquote><p>对于图片、视频类型的数据，会默认开启压缩机制，因此一般无需再次开启压缩。</p><p>对于.js文件而言，需要指定压缩类型为application/javascript，而并非text/javascript、application/x-javascript</p><p>配置抽取：这些配置在很多地方可能都会用到，所以我们可以将这些内容抽取到一个配置文件中，然后通过include指令把配置文件再次加载到nginx.conf配置文件中，方法使用。</p></blockquote><h2 id="gzip和sendfile共存问题" tabindex="-1">Gzip和sendfile共存问题 <a class="header-anchor" href="#gzip和sendfile共存问题" aria-label="Permalink to &quot;Gzip和sendfile共存问题&quot;">​</a></h2><blockquote><p>前面在讲解sendfile的时候，提到过，开启sendfile以后，在读取磁盘上的静态资源文件的时候，可以减少拷贝的次数，可以<strong>不经过用户进程将静态文件通过网络设备发送出去</strong>，但是Gzip要想对资源压缩，是<strong>需要经过用户进程进行操作</strong>的。所以如何解决两个设置的共存问题。</p></blockquote><p>可以使用ngx_http_gzip_static_module模块的gzip_static指令来解决。</p><p>gzip_static: 检查与访问资源同名的.gz文件时，response中以gzip相关的header返回.gz文件的内容。</p><table><thead><tr><th>语法</th><th><strong>gzip_static</strong> on | off | always;</th></tr></thead><tbody><tr><td>默认值</td><td>gzip_static off;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><p>添加上述命令后，会报一个错误，<code>unknown directive &quot;gzip_static&quot;</code>主要的原因是Nginx默认是没有添加ngx_http_gzip_static_module模块。如何来添加?</p><h3 id="gzip-static-模块添加" tabindex="-1">gzip_static 模块添加 <a class="header-anchor" href="#gzip-static-模块添加" aria-label="Permalink to &quot;gzip_static 模块添加&quot;">​</a></h3><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 查询当前Nginx的配置参数</span></span>
<span class="line"><span style="color:#FFCB6B;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-v</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 将nginx安装目录下sbin目录中的nginx二进制文件进行更名</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/nginx/sbin</span></span>
<span class="line"><span style="color:#FFCB6B;">mv</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">nginxold</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 进入Nginx的安装目录</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/nginx-1.23.2</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 执行make clean清空之前编译的内容</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">clean</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 使用configure来配置参数</span></span>
<span class="line"><span style="color:#FFCB6B;">./configure</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--with-http_gzip_static_module</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 使用make命令进行编译</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 将objs目录下的nginx二进制执行文件移动到nginx安装目录下的sbin目录中</span></span>
<span class="line"><span style="color:#FFCB6B;">mv</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">objs/nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/nginx/sbin</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 执行更新命令</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">upgrade</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 查看是否安装成功</span></span>
<span class="line"><span style="color:#FFCB6B;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-V</span></span></code></pre></div><h3 id="gzip-static测试使用" tabindex="-1">gzip_static测试使用 <a class="header-anchor" href="#gzip-static测试使用" aria-label="Permalink to &quot;gzip_static测试使用&quot;">​</a></h3><p>直接访问<code>http://192.168.200.133/jquery.js</code></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204281813524.png" alt="1587932106429" style="zoom:67%;"><p>(2)使用gzip命令进行压缩</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">cd </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">nginx</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span></span>
<span class="line"><span style="color:#A6ACCD;">gzip jquery</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">js</span></span></code></pre></div><p>(3)再次访问<code>http://192.168.200.133/jquery.js</code></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204281813528.png" alt="1587932300006" style="zoom:67%;"><h1 id="静态资源缓存-⭐" tabindex="-1">静态资源缓存 ⭐ <a class="header-anchor" href="#静态资源缓存-⭐" aria-label="Permalink to &quot;静态资源缓存 ⭐&quot;">​</a></h1><h2 id="什么是缓存" tabindex="-1">什么是缓存 <a class="header-anchor" href="#什么是缓存" aria-label="Permalink to &quot;什么是缓存&quot;">​</a></h2><blockquote><p>缓存（cache），原始意义是指访问速度比一般随机存取存储器（RAM）快的一种高速存储器，通常它不像系统主存那样使用DRAM技术，而使用昂贵但较快速的SRAM技术。缓存的设置是所有现代计算机系统发挥高性能的重要因素之一。</p></blockquote><h2 id="什么是web缓存" tabindex="-1">什么是web缓存 <a class="header-anchor" href="#什么是web缓存" aria-label="Permalink to &quot;什么是web缓存&quot;">​</a></h2><blockquote><p>Web缓存是指一个Web资源（如html页面，图片，js，数据等）存在于Web服务器和客户端（浏览器）之间的副本。缓存会根据进来的请求保存输出内容的副本；当下一个请求来到的时候，如果是相同的URL，缓存会根据缓存机制决定是直接使用副本响应访问请求，还是向源服务器再次发送请求。比较常见的就是浏览器会缓存访问过网站的网页，当再次访问这个URL地址的时候，如果网页没有更新，就不会再次下载网页，而是直接使用本地缓存的网页。只有当网站明确标识资源已经更新，浏览器才会再次下载网页</p></blockquote><h2 id="web缓存详解" tabindex="-1">web缓存详解 <a class="header-anchor" href="#web缓存详解" aria-label="Permalink to &quot;web缓存详解&quot;">​</a></h2><p>客户端缓存：浏览器缓存；服务端缓存：Nginx / Redis / Memcached等</p><h3 id="浏览器缓存" tabindex="-1">浏览器缓存 <a class="header-anchor" href="#浏览器缓存" aria-label="Permalink to &quot;浏览器缓存&quot;">​</a></h3><blockquote><p>是为了节约网络的资源加速浏览，浏览器在用户磁盘上对最近请求过的文档进行存储，当访问者再次请求这个页面时，浏览器就可以从本地磁盘显示文档，这样就可以加速页面的阅览.</p></blockquote><h3 id="为什么要用浏览器缓存" tabindex="-1">为什么要用浏览器缓存 <a class="header-anchor" href="#为什么要用浏览器缓存" aria-label="Permalink to &quot;为什么要用浏览器缓存&quot;">​</a></h3><ul><li>成本最低的一种缓存实现</li><li>减少网络带宽消耗</li><li>降低服务器压力</li><li>减少网络延迟，加快页面打开速度</li></ul><h3 id="浏览器缓存的执行流程" tabindex="-1">浏览器缓存的执行流程 <a class="header-anchor" href="#浏览器缓存的执行流程" aria-label="Permalink to &quot;浏览器缓存的执行流程&quot;">​</a></h3><p>HTTP协议中和页面缓存相关的字段，我们先来认识下：</p><table><thead><tr><th>header</th><th>说明</th></tr></thead><tbody><tr><td>Expires</td><td>缓存过期的日期和时间</td></tr><tr><td>Cache-Control</td><td>设置和缓存相关的配置信息</td></tr><tr><td>Last-Modified</td><td>请求资源最后修改时间</td></tr><tr><td>ETag</td><td>请求变量的实体标签的当前值，比如文件的MD5值</td></tr></tbody></table><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204281823462.png" alt=""></p><p>（1）用户首次通过浏览器发送请求到服务端获取数据，客户端是没有对应的缓存，所以需要发送request请求获取数据</p><p>（2）服务端接收到请求后，获取服务端的数据及服务端缓存的允许后，返回200的成功状态码并且在响应头上附上对应资源以及缓存信息；</p><p>（3）当用户再次访问相同资源的时候，客户端会在浏览器的缓存目录中查找是否存在响应的缓存文件</p><p>（4）如果没有找到对应的缓存文件，则走(2)步</p><p>（5）如果有缓存文件，接下来对缓存文件是否过期进行判断，过期的判断标准是(Expires),</p><p>（6）如果没有过期，则直接从本地缓存中返回数据进行展示</p><p>（7）如果Expires过期，接下来需要判断缓存文件是否发生过变化</p><p>（8）判断的标准有两个，一个是ETag(Entity Tag),一个是Last-Modified</p><p>（9）判断结果是未发生变化，则服务端返回304，直接从缓存文件中获取数据</p><p>（10）如果判断是发生了变化，重新从服务端获取数据，并根据缓存协商(服务端所设置的是否需要进行缓存数据的设置)来进行数据缓存。</p><h2 id="浏览器缓存指令" tabindex="-1">浏览器缓存指令 <a class="header-anchor" href="#浏览器缓存指令" aria-label="Permalink to &quot;浏览器缓存指令&quot;">​</a></h2><p>Nginx需要进行缓存相关设置，就需要用到如下的指令</p><h3 id="expires指令" tabindex="-1">expires指令 <a class="header-anchor" href="#expires指令" aria-label="Permalink to &quot;expires指令&quot;">​</a></h3><p>expires:该指令用来控制页面缓存的作用。可以通过该指令控制HTTP应答中的“Expires&quot;和”Cache-Control&quot;</p><table><thead><tr><th>语法</th><th>expires [modified] time expires epoch|max|off;</th></tr></thead><tbody><tr><td>默认值</td><td>expires off;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><ul><li>time:可以整数也可以是负数，指定过期时间，如果是负数，Cache-Control则为no-cache,如果为整数或0，则Cache-Control的值为max-age=time;</li><li>epoch: 指定Expires的值为&#39;1 January,1970,00:00:01 GMT&#39;(1970-01-01 00:00:00)，Cache-Control的值no-cache</li><li>max:指定Expires的值为&#39;31 December2037 23:59:59GMT&#39; (2037-12-31 23:59:59) ，Cache-Control的值为10年</li><li>off:默认不缓存。</li></ul><h3 id="add-header指令" tabindex="-1">add_header指令 <a class="header-anchor" href="#add-header指令" aria-label="Permalink to &quot;add_header指令&quot;">​</a></h3><p>add_header指令是用来添加指定的响应头和响应值。</p><table><thead><tr><th>语法</th><th>add_header name value [always];</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>http、server、location...</td></tr></tbody></table><p>Cache-Control作为响应头信息，可以设置如下值：</p><p>缓存响应指令：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">Cache</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">control: must</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">revalidate</span></span>
<span class="line"><span style="color:#A6ACCD;">Cache</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">control: no</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">cache</span></span>
<span class="line"><span style="color:#A6ACCD;">Cache</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">control: no</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">store</span></span>
<span class="line"><span style="color:#A6ACCD;">Cache</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">control: no</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">transform</span></span>
<span class="line"><span style="color:#A6ACCD;">Cache</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">control: public</span></span>
<span class="line"><span style="color:#A6ACCD;">Cache</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">control: private</span></span>
<span class="line"><span style="color:#A6ACCD;">Cache</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">control: proxy</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">revalidate</span></span>
<span class="line"><span style="color:#A6ACCD;">Cache</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control: max</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">age</span><span style="color:#89DDFF;">=&lt;</span><span style="color:#A6ACCD;">seconds</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">Cache</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">control: s</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">maxage</span><span style="color:#89DDFF;">=&lt;</span><span style="color:#A6ACCD;">seconds</span><span style="color:#89DDFF;">&gt;</span></span></code></pre></div><table><thead><tr><th>指令</th><th>说明</th></tr></thead><tbody><tr><td>must-revalidate</td><td>可缓存但必须再向源服务器进行确认</td></tr><tr><td>no-cache</td><td>缓存前必须确认其有效性</td></tr><tr><td>no-store</td><td>不缓存请求或响应的任何内容</td></tr><tr><td>no-transform</td><td>代理不可更改媒体类型</td></tr><tr><td>public</td><td>可向任意方提供响应的缓存</td></tr><tr><td>private</td><td>仅向特定用户返回响应</td></tr><tr><td>proxy-revalidate</td><td>要求中间缓存服务器对缓存的响应有效性再进行确认</td></tr><tr><td>max-age=&lt;秒&gt;</td><td>响应最大Age值</td></tr><tr><td>s-maxage=&lt;秒&gt;</td><td>公共缓存服务器响应的最大Age值</td></tr></tbody></table><h2 id="浏览器自带缓存演示" tabindex="-1">浏览器自带缓存演示 <a class="header-anchor" href="#浏览器自带缓存演示" aria-label="Permalink to &quot;浏览器自带缓存演示&quot;">​</a></h2><blockquote><p>ctrl+shift+del，清空浏览器缓存</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301161657904.png" alt="image-20230116165705811" style="zoom:67%;"><p>第一次访问</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301161654631.png" alt="image-20230116165409518" style="zoom:67%;"><p>后续访问</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301161654556.png" alt="image-20230116165454450" style="zoom:67%;"><h2 id="静态资源添加缓存完整配置-⭐" tabindex="-1">静态资源添加缓存完整配置 ⭐ <a class="header-anchor" href="#静态资源添加缓存完整配置-⭐" aria-label="Permalink to &quot;静态资源添加缓存完整配置 ⭐&quot;">​</a></h2><p>未开启缓存前</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204281836988.png" alt="image-20220428183607876" style="zoom:80%;"><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 资源路径</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   root </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">nginx</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   index index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 加上静态资源缓存</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 注意：资源路径不是</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">的情况下，配置有可能不生效，而且会出现资源无法访问的情况</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 嵌套在网页内的js、css、img也是会生效的比如vue页面</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">~*</span><span style="color:#A6ACCD;"> ^</span><span style="color:#89DDFF;">.+.(</span><span style="color:#A6ACCD;">jpg</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">jpeg</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">gif</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">css</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">png</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">js</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">ico</span><span style="color:#89DDFF;">)</span><span style="color:#A6ACCD;">$ </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 资源要给个根路径，不然访问不了</span><span style="color:#F78C6C;">404</span></span>
<span class="line"><span style="color:#A6ACCD;">   root html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   expires </span><span style="color:#F78C6C;">30d</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   add_header Pragma public</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   add_header Cache</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">public</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>访问图片：<a href="http://192.168.22.130:8089/g2.jpg" target="_blank" rel="noreferrer">http://192.168.22.130:8089/g2.jpg</a> 注意：单位是秒</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206231405759.png" alt="image-20220623140536613" style="zoom:67%;"><h1 id="跨域" tabindex="-1">跨域 <a class="header-anchor" href="#跨域" aria-label="Permalink to &quot;跨域&quot;">​</a></h1><p>跨域问题在之前的单体架构开发中，其实是比较少见的问题，除非是需要接入第三方<code>SDK</code>时，才需要处理此问题。但随着现在前后端分离、分布式架构的流行，跨域问题也成为了每个Java开发必须要懂得解决的一个问题。<a href="https://mp.weixin.qq.com/s?__biz=MzI4Njc5NjM1NQ==&amp;mid=2247540345&amp;idx=2&amp;sn=88303eec78028e75b016927dc20628a7&amp;chksm=ebd56155dca2e843435e830e3a5fe42e75932a9414b67e4a3bbfb6bc8f6c1f58d81caeaba390&amp;mpshare=1&amp;scene=23&amp;srcid=0604OEry4ZJVZWPXhaREaDHD&amp;sharer_sharetime=1685854478207&amp;sharer_shareid=29b8a04db1dbd975e3bf4e9f47e7ac67#rd" target="_blank" rel="noreferrer">跨域问题分析和解决</a></p><h2 id="跨域和同源策略" tabindex="-1">跨域和同源策略 <a class="header-anchor" href="#跨域和同源策略" aria-label="Permalink to &quot;跨域和同源策略&quot;">​</a></h2><h3 id="跨域问题产生原因" tabindex="-1">跨域问题产生原因 <a class="header-anchor" href="#跨域问题产生原因" aria-label="Permalink to &quot;跨域问题产生原因&quot;">​</a></h3><p>产生跨域问题的主要原因就在于**「同源策略」** ，为了保证用户信息安全，防止恶意网站窃取数据，同源策略是必须的，否则<code>cookie</code>可以共享。由于<code>http</code>无状态协议通常会借助<code>cookie</code>来实现有状态的信息记录，例如用户的身份/密码等，因此一旦<code>cookie</code>被共享，那么会导致用户的身份信息被盗取。同源策略主要是指三点相同，<strong>「协议+域名+端口」</strong> 相同的两个请求，则可以被看做是同源的，但如果其中任意一点存在不同，则代表是两个不同源的请求，同源策略会限制了不同源之间的资源交互。</p><h3 id="同源策略" tabindex="-1">同源策略 <a class="header-anchor" href="#同源策略" aria-label="Permalink to &quot;同源策略&quot;">​</a></h3><p>浏览器的同源策略：是一种约定，是浏览器最核心也是最基本的安全功能，如果浏览器少了同源策略，则浏览器的正常功能可能都会受到影响。<code>同源: 协议、域名(IP)、端口相同即为同源</code></p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">http://192.168.200.131/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">https://192.168.200.131/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">不</span></span>
<span class="line"></span>
<span class="line"><span style="color:#FFCB6B;">http://192.168.200.131/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">http://192.168.200.132/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">不</span></span>
<span class="line"></span>
<span class="line"><span style="color:#FFCB6B;">http://192.168.200.131/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">http://192.168.200.131:8080/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">不</span></span>
<span class="line"></span>
<span class="line"><span style="color:#FFCB6B;">http://www.nginx.com/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">http://www.nginx.org/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">不</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#FFCB6B;">http://192.168.200.131/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">http://192.168.200.131:8080/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">不</span></span>
<span class="line"></span>
<span class="line"><span style="color:#FFCB6B;">http://www.nginx.org:80/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">http://www.nginx.org/user/1</span></span>
<span class="line"><span style="color:#FFCB6B;">满足(因为端口默认就是80</span><span style="color:#A6ACCD;">)</span></span></code></pre></div><h2 id="跨域问题重现" tabindex="-1">跨域问题重现 <a class="header-anchor" href="#跨域问题重现" aria-label="Permalink to &quot;跨域问题重现&quot;">​</a></h2><p>出现跨域问题会有什么效果?,接下来通过一个需求来给大家演示下：</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204281823435.png" style="zoom:80%;">（1）nginx的html目录下新建一个a.html</p><div class="language-html"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">html</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">head</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">meta</span><span style="color:#89DDFF;"> </span><span style="color:#C792EA;">charset</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">utf-8</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">title</span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;">跨域问题演示</span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">title</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#89DDFF;">        &lt;</span><span style="color:#F07178;">script</span><span style="color:#89DDFF;"> </span><span style="color:#C792EA;">src</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">jquery.js</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">&gt;&lt;/</span><span style="color:#F07178;">script</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#89DDFF;">        &lt;</span><span style="color:#F07178;">script</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#82AAFF;">$</span><span style="color:#A6ACCD;">(</span><span style="color:#C792EA;">function</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#F07178;">                </span><span style="color:#82AAFF;">$</span><span style="color:#F07178;">(</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">#btn</span><span style="color:#89DDFF;">&quot;</span><span style="color:#F07178;">)</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">click</span><span style="color:#F07178;">(</span><span style="color:#C792EA;">function</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#F07178;">                  </span><span style="color:#A6ACCD;">$</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">get</span><span style="color:#F07178;">(</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">http://192.168.22.146:8080/getUser</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">,</span><span style="color:#C792EA;">function</span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;font-style:italic;">data</span><span style="color:#89DDFF;">){</span></span>
<span class="line"><span style="color:#F07178;">                        </span><span style="color:#82AAFF;">alert</span><span style="color:#F07178;">(</span><span style="color:#A6ACCD;">JSON</span><span style="color:#89DDFF;">.</span><span style="color:#82AAFF;">stringify</span><span style="color:#F07178;">(</span><span style="color:#A6ACCD;">data</span><span style="color:#F07178;">))</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F07178;">                  </span><span style="color:#89DDFF;">}</span><span style="color:#F07178;">)</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F07178;">                </span><span style="color:#89DDFF;">}</span><span style="color:#F07178;">)</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F07178;">            </span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">)</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">script</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">head</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">body</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&lt;</span><span style="color:#F07178;">input</span><span style="color:#89DDFF;"> </span><span style="color:#C792EA;">type</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">button</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;"> </span><span style="color:#C792EA;">value</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">获取数据</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;"> </span><span style="color:#C792EA;">id</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">btn</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">/&gt;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">body</span><span style="color:#89DDFF;">&gt;</span></span>
<span class="line"><span style="color:#89DDFF;">&lt;/</span><span style="color:#F07178;">html</span><span style="color:#89DDFF;">&gt;</span></span></code></pre></div><p>（2）在nginx.conf配置如下内容</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen  </span><span style="color:#F78C6C;">8080</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">getUser</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        default_type application</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">json</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{&quot;id&quot;:1,&quot;name&quot;:&quot;TOM&quot;,&quot;age&quot;:18}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen 	</span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">		root html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		index index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>(3)通过浏览器访问测试</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204281823436.png" alt="1588004913681"></p><h2 id="解决方案" tabindex="-1">解决方案 <a class="header-anchor" href="#解决方案" aria-label="Permalink to &quot;解决方案&quot;">​</a></h2><p>使用add_header指令，该指令可以用来添加一些头信息</p><p>此处用来解决跨域问题，需要添加两个头信息，一个是<code>Access-Control-Allow-Origin</code>,<code>Access-Control-Allow-Methods</code>。Access-Control-Allow-Origin: 直译过来是允许跨域访问的源地址信息，可以配置多个(多个用逗号分隔)，也可以使用<code>*</code>代表所有源。Access-Control-Allow-Methods:直译过来是允许跨域访问的请求方式，值可以为 GET POST PUT DELETE...,可以全部设置，也可以根据需要设置，多个用逗号分隔</p><h3 id="最简单配置" tabindex="-1">最简单配置 <a class="header-anchor" href="#最简单配置" aria-label="Permalink to &quot;最简单配置&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 一般设置在后端发起请求部分</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Origin </span><span style="color:#89DDFF;">*;</span></span>
<span class="line"><span style="color:#A6ACCD;">    add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Methods GET</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">POST</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">PUT</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">DELETE</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    default_type application</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">json</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{&quot;id&quot;:1,&quot;name&quot;:&quot;TOM&quot;,&quot;age&quot;:18}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="详细配置" tabindex="-1">详细配置 <a class="header-anchor" href="#详细配置" aria-label="Permalink to &quot;详细配置&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen  </span><span style="color:#F78C6C;">8080</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 允许 所有头部 所有域 所有方法</span></span>
<span class="line"><span style="color:#A6ACCD;">  		add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Origin </span><span style="color:#89DDFF;">*;</span></span>
<span class="line"><span style="color:#A6ACCD;"> 		add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Headers </span><span style="color:#89DDFF;">*;</span></span>
<span class="line"><span style="color:#A6ACCD;">  		add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Methods </span><span style="color:#89DDFF;">*;</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> OPTIONS 直接返回</span><span style="color:#F78C6C;">204</span></span>
<span class="line"><span style="color:#A6ACCD;">  		if ($request_method </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">OPTIONS</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">  			return </span><span style="color:#F78C6C;">204</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">		default_type application</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">json</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   		return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{&quot;id&quot;:1,&quot;name&quot;:&quot;TOM&quot;,&quot;age&quot;:18}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="更详细配置" tabindex="-1">更详细配置 <a class="header-anchor" href="#更详细配置" aria-label="Permalink to &quot;更详细配置&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 允许跨域的请求，可以自定义变量$http_origin，</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">表示所有</span></span>
<span class="line"><span style="color:#A6ACCD;">    add_header </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Access-Control-Allow-Origin</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 允许携带cookie请求</span></span>
<span class="line"><span style="color:#A6ACCD;">    add_header </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Access-Control-Allow-Credentials</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">true</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 允许跨域请求的方法：GET</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">POST</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">OPTIONS</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">PUT</span></span>
<span class="line"><span style="color:#A6ACCD;">    add_header </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Access-Control-Allow-Methods</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">GET,POST,OPTIONS,PUT</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 允许请求时携带的头部信息，</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">表示所有</span></span>
<span class="line"><span style="color:#A6ACCD;">    add_header </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Access-Control-Allow-Headers</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">*;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 允许发送按段获取资源的请求</span></span>
<span class="line"><span style="color:#A6ACCD;">    add_header </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Access-Control-Expose-Headers</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Content-Length,Content-Range</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 一定要有！！！否则Post请求无法进行跨域！</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 在发送Post跨域请求前，会以Options方式发送预检请求，服务器接受时才会正式请求</span></span>
<span class="line"><span style="color:#A6ACCD;">    if ($request_method </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">OPTIONS</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        add_header </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Access-Control-Max-Age</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1728000</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        add_header </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Content-Type</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">text/plain; charset=utf-8</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        add_header </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">Content-Length</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 对于Options方式的请求返回</span><span style="color:#F78C6C;">204</span><span style="color:#A6ACCD;">，表示接受跨域请求</span></span>
<span class="line"><span style="color:#A6ACCD;">        return </span><span style="color:#F78C6C;">204</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    default_type application</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">json</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{&quot;id&quot;:1,&quot;name&quot;:&quot;TOM&quot;,&quot;age&quot;:18}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="超详细配置⭐" tabindex="-1">超详细配置⭐ <a class="header-anchor" href="#超详细配置⭐" aria-label="Permalink to &quot;超详细配置⭐&quot;">​</a></h3><p>再贴一份完整配置（<code>*</code>号根据自己‘喜好’填写）：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        listen       </span><span style="color:#F78C6C;">22222</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        server_name  localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        location  </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">            if ($request_method </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">OPTIONS</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">                add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Origin </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">:8080</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">                add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Headers </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">*</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">                add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Methods </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">*</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">                add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Credentials </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">true</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">                return </span><span style="color:#F78C6C;">204</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">            if ($request_method </span><span style="color:#89DDFF;">!=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">OPTIONS</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">                add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Origin </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">:8080</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> always</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">                add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Credentials </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">true</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">            proxy_pass  :</span><span style="color:#F78C6C;">59200</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span></code></pre></div><p>或者：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        listen       </span><span style="color:#F78C6C;">22222</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        server_name  localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        location  </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">            add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Origin </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">:8080</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> always</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">            add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Headers </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">*</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">            add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Methods </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">*</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">            add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Credentials </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">true</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">            if ($request_method </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">OPTIONS</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">                return </span><span style="color:#F78C6C;">204</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">            proxy_pass  :</span><span style="color:#F78C6C;">59200</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span></code></pre></div><h2 id="响应头分析" tabindex="-1">响应头分析 <a class="header-anchor" href="#响应头分析" aria-label="Permalink to &quot;响应头分析&quot;">​</a></h2><p><strong>跨域主要涉及4个响应头：</strong></p><ul><li><code>Access-Control-Allow-Origin</code> 用于设置允许跨域请求源地址 （预检请求和正式请求在跨域时候都会验证）</li><li><code>Access-Control-Allow-Headers</code> 跨域允许携带的特殊头信息字段 （只在预检请求验证）</li><li><code>Access-Control-Allow-Methods</code> 跨域允许的请求方法或者说HTTP动词 （只在预检请求验证）</li><li><code>Access-Control-Allow-Credentials</code> 是否允许跨域使用cookies，如果要跨域使用cookies，可以添加上此请求响应头，值设为true（设置或者不设置，都不会影响请求发送，只会影响在跨域时候是否要携带cookies，但是如果设置，预检请求和正式请求都需要设置）。不过不建议跨域使用（项目中用到过，不过不稳定，有些浏览器带不过去），除非必要，因为有很多方案可以代替。</li></ul><p>网上很多文章都是告诉你直接Nginx添加这几个响应头信息就能解决跨域，当然大部分情况是能解决，但是我相信还是有很多情况，明明配置上了，也同样会报跨域问题。</p><p>什么是预检请求？：当发生跨域条件时候，览器先询问服务器，当前网页所在的域名是否在服务器的许可名单之中，以及可以使用哪些HTTP动词和头信息字段。只有得到肯定答复，浏览器才会发出正式的<code>XMLHttpRequest</code>请求，否则就报错。</p><h1 id="防盗链" tabindex="-1">防盗链 <a class="header-anchor" href="#防盗链" aria-label="Permalink to &quot;防盗链&quot;">​</a></h1><h2 id="什么是资源盗链" tabindex="-1">什么是资源盗链 <a class="header-anchor" href="#什么是资源盗链" aria-label="Permalink to &quot;什么是资源盗链&quot;">​</a></h2><blockquote><p>资源盗链指的是此内容不在自己服务器上，而是通过技术手段，绕过别人的限制将别人的内容放到自己页面上最终展示给用户。以此来盗取大网站的空间和流量。简而言之就是<strong>用别人的东西成就自己的网站</strong></p></blockquote><p>首先了解一下何谓盗链：<strong>「盗链即是指外部网站引入当前网站的资源对外展示」</strong> ，来举个简单的例子理解：</p><blockquote><p>好比壁纸网站<code>X</code>站、<code>Y</code>站，<code>X</code>站是一点点去购买版权、签约作者的方式，从而积累了海量的壁纸素材，但<code>Y</code>站由于资金等各方面的原因，就直接通过<code>&lt;img src=&quot;X站/xxx.jpg&quot; /&gt;</code>这种方式照搬了<code>X</code>站的所有壁纸资源，继而提供给用户下载。</p></blockquote><p>那么如果我们自己是这个<code>X</code>站的<code>Boss</code>，心中必然不爽，那么此时又该如何屏蔽这类问题呢？「防盗链」登场了！</p><h2 id="nginx-防盗链" tabindex="-1">Nginx 防盗链 <a class="header-anchor" href="#nginx-防盗链" aria-label="Permalink to &quot;Nginx 防盗链&quot;">​</a></h2><blockquote><p>Nginx的防盗链机制实现，跟一个头部字段：<code>Referer</code>有关，该字段主要描述了当前请求是从哪儿发出的，那么在<code>Nginx</code>中就可获取该值，然后判断是否为本站的资源引用请求，如果不是则不允许访问。</p></blockquote><blockquote><p>Nginx会通就过查看referer自动和valid_referers后面的内容进行匹配，如果匹配到了就将$invalid_referer变量置0，如果没有匹配到，则将$invalid_referer变量置为1，匹配的过程中不区分大小写。</p></blockquote><p><code>Nginx</code>中存在一个配置项为<code>valid_referers</code>，正好可以满足前面的需求，语法如下：</p><ul><li><p><code>valid_referers none | blocked | server_names | string ...;</code></p></li><li><ul><li><code>none</code>：表示接受没有<code>Referer</code>字段的<code>HTTP</code>请求访问。</li></ul></li><li><p><code>blocked</code>：表示允许<code>http://</code>或<code>https//</code>以外的请求访问。</p></li><li><p><code>server_names</code>：资源的白名单，这里可以指定允许访问的域名。</p></li><li><p><code>string</code>：可自定义字符串，支配通配符、正则表达式写法。</p></li></ul><h2 id="防盗链实现" tabindex="-1">防盗链实现 <a class="header-anchor" href="#防盗链实现" aria-label="Permalink to &quot;防盗链实现&quot;">​</a></h2><p>简单了解语法后，接下来的实现如下：</p><div class="language-yaml"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 在动静分离的location中开启防盗链机制</span></span>
<span class="line"><span style="color:#C3E88D;">location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;"># 最后面的值在上线前可配置为允许的域名地址</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">valid_referers blocked 192.168.22.146 www.baidu.com;</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;"># if后面一定要加上空格</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">if ($invalid_referer) {</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;"># 可以配置成返回一张禁止盗取的图片</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;"># rewrite   ^/ http://xx.xx.com/NO.jpg;</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;"># 也可直接返回403，对应下面的error_page 403，这样能跳转错误页面</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C3E88D;">return   403;</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">root  /usr/local/nginx/html;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">expires 7d;</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><p>访问测试：<a href="http://192.168.22.146/img/y2.jpg" target="_blank" rel="noreferrer">http://192.168.22.146/img/y2.jpg</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301161810975.png" alt="image-20230116181012883" style="zoom:67%;"><blockquote><p>如果在valid_referers后面再跟上none，那么即可正常访问图片</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301161813207.png" alt="image-20230116181304426" style="zoom:50%;"><p>Referer的限制比较粗，比如随意加一个Referer，上面的方式是无法进行限制的。那么这个问题改如何解决？此处我们需要用到Nginx的第三方模块<code>ngx_http_accesskey_module</code>，第三方模块如何实现盗链，如果在Nginx中使用第三方模块的功能，这些我们在后面的Nginx的模块篇再进行详细的讲解。</p><blockquote><p>PS：防盗链机制也无法解决爬虫伪造<code>referers</code>信息的这种方式抓取数据。</p></blockquote><h1 id="全局变量和语法⭐" tabindex="-1">全局变量和语法⭐ <a class="header-anchor" href="#全局变量和语法⭐" aria-label="Permalink to &quot;全局变量和语法⭐&quot;">​</a></h1><h2 id="常用全局变量-⭐" tabindex="-1">常用全局变量 ⭐ <a class="header-anchor" href="#常用全局变量-⭐" aria-label="Permalink to &quot;常用全局变量 ⭐&quot;">​</a></h2><h3 id="全局变量" tabindex="-1">全局变量 <a class="header-anchor" href="#全局变量" aria-label="Permalink to &quot;全局变量&quot;">​</a></h3><blockquote><p>nginx的配置文件中可以使用的内置变量以美元符<code>$</code>开始，也有人叫全局变量。其中，部分预定义的变量的值是可以改变的。</p></blockquote><table><thead><tr><th>变量</th><th>说明</th></tr></thead><tbody><tr><td>$args</td><td>变量中存放了请求URL中的请求参数。比如<a href="http://192.168.200.133/server?arg1=value1&amp;args2=value2%E4%B8%AD%E7%9A%84%22arg1=value1&amp;arg2=value2%22%EF%BC%8C%E5%8A%9F%E8%83%BD%E5%92%8C$query_string%E4%B8%80%E6%A0%B7" target="_blank" rel="noreferrer">http://192.168.200.133/server?arg1=value1&amp;args2=value2中的&quot;arg1=value1&amp;arg2=value2&quot;，功能和$query_string一样</a></td></tr><tr><td>$http_user_agent</td><td>存储的是用户访问服务的代理信息(如果通过浏览器访问，记录的是浏览器的相关版本信息)</td></tr><tr><td>$host</td><td>变量存储的是访问服务器的server_name值</td></tr><tr><td>$document_uri</td><td>变量存储的是当前访问地址的URI。比如<a href="http://192.168.200.133/server?id=10&amp;name=zhangsan%E4%B8%AD%E7%9A%84%22/server%22%EF%BC%8C%E5%8A%9F%E8%83%BD%E5%92%8C$uri%E4%B8%80%E6%A0%B7" target="_blank" rel="noreferrer">http://192.168.200.133/server?id=10&amp;name=zhangsan中的&quot;/server&quot;，功能和$uri一样</a></td></tr><tr><td>$document_root</td><td>变量存储的是当前请求对应location的root值，如果未设置，默认指向Nginx自带html目录所在位置</td></tr><tr><td>$content_length</td><td>变量存储的是请求头中的Content-Length的值</td></tr><tr><td>$content_type</td><td>变量存储的是请求头中的Content-Type的值</td></tr><tr><td>$http_cookie</td><td>变量存储的是客户端的cookie信息，可以通过add_header Set-Cookie &#39;cookieName=cookieValue&#39;来添加cookie数据</td></tr><tr><td>$limit_rate</td><td>变量中存储的是Nginx服务器对网络连接速率的限制，也就是Nginx配置中对limit_rate指令设置的值，默认是0，不限制。</td></tr><tr><td>$remote_addr</td><td>变量中存储的是客户端的IP地址</td></tr><tr><td>$remote_port</td><td>变量中存储了客户端与服务端建立连接的端口号</td></tr><tr><td>$remote_user</td><td>变量中存储了客户端的用户名，需要有认证模块才能获取</td></tr><tr><td>$scheme</td><td>变量中存储了访问协议</td></tr><tr><td>$server_addr</td><td>变量中存储了服务端的地址</td></tr><tr><td>$server_name</td><td>变量中存储了客户端请求到达的服务器的名称</td></tr><tr><td>$server_port</td><td>变量中存储了客户端请求到达服务器的端口号</td></tr><tr><td>$server_protocol</td><td>变量中存储了客户端请求协议的版本，比如&quot;HTTP/1.1&quot;</td></tr><tr><td>$request_body_file</td><td>变量中存储了发给后端服务器的本地文件资源的名称</td></tr><tr><td>$request_method</td><td>变量中存储了客户端的请求方式，比如&quot;GET&quot;,&quot;POST&quot;等</td></tr><tr><td>$request_filename</td><td>变量中存储了当前请求的资源文件的路径名</td></tr><tr><td>$request_uri</td><td>变量中存储了当前请求的URI，并且携带请求参数，比如<a href="http://192.168.200.133/server?id=10&amp;name=zhangsan%E4%B8%AD%E7%9A%84%22/server?id=10&amp;name=zhangsan%22" target="_blank" rel="noreferrer">http://192.168.200.133/server?id=10&amp;name=zhangsan中的&quot;/server?id=10&amp;name=zhangsan&quot;</a></td></tr></tbody></table><h3 id="全局变量使用示例" tabindex="-1">全局变量使用示例 <a class="header-anchor" href="#全局变量使用示例" aria-label="Permalink to &quot;全局变量使用示例&quot;">​</a></h3><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C3E88D;">server {</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">listen  8081;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">location /server {</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">set $name TOM;</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">set $age 18;</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">default_type text/plain;</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">return 200 $name=$age=$args;</span></span>
<span class="line"><span style="color:#A6ACCD;">	}</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><p><a href="http://192.168.22.129:8081/server?username=qwe&amp;gender=1" target="_blank" rel="noreferrer">http://192.168.22.129:8081/server?username=qwe&amp;gender=1</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204291450658.png" alt="image-20220429145059562" style="zoom:67%;"><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C3E88D;">server {</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">listen  8081;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">location /server {</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">default_type text/plain;</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">return 200 $args=$http_user_agent=$remote_addr;</span></span>
<span class="line"><span style="color:#A6ACCD;">	}</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><p><a href="http://192.168.22.129:8081/server?username=qwe&amp;gender=1" target="_blank" rel="noreferrer">http://192.168.22.129:8081/server?username=qwe&amp;gender=1</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204291455338.png" alt="image-20220429145546255" style="zoom:80%;"><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C3E88D;">server {</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">listen 80;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">server_name localhost;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">location /getVar {</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;"># default_type &quot;text/html&quot;;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C3E88D;">return 200 &quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">remote_addr</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$remote_addr</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">remote_port</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$remote_port</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">server_addr</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$server_addr</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">server_port</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$server_port</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">server_protocol</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$server_protocol</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">binary_remote_addr</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$binary_remote_addr</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">connection</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$connection</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">uri</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$uri</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">request_uri</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$request_uri</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">scheme</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$scheme</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">request_method</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$request_method</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">request_length</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$request_length</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">args</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$args</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">arg_pid</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$arg_pid</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">is_args</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$is_args</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">query_string</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$query_string</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">host</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$host</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">http_user_agent</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$http_user_agent</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">http_referer</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$http_referer</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">http_via</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$http_via</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">request_time</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$request_time</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">https</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$https</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">request_filename</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$request_filename</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#F07178;">document_root</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">$document_root</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">;</span></span>
<span class="line"><span style="color:#C3E88D;">     }</span></span>
<span class="line"><span style="color:#C3E88D;">}</span></span></code></pre></div><p>访问官网：<a href="http://192.168.22.130/getVar" target="_blank" rel="noreferrer">http://192.168.22.130/getVar</a></p><p>由于 <code>Nginx</code> 中写了 <code>return</code> 方法，因此 <code>chrome</code> 浏览器会默认为我们下载一个文件，下面下载的文件内容</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">remote_addr:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">192.168</span><span style="color:#C3E88D;">.22.1</span></span>
<span class="line"><span style="color:#FFCB6B;">remote_port:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">13189</span></span>
<span class="line"><span style="color:#FFCB6B;">server_addr:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">192.168</span><span style="color:#C3E88D;">.22.130</span></span>
<span class="line"><span style="color:#FFCB6B;">server_port:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">80</span></span>
<span class="line"><span style="color:#FFCB6B;">server_protocol:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">HTTP/1.1</span></span>
<span class="line"><span style="color:#FFCB6B;">binary_remote_addr:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">括</span></span>
<span class="line"><span style="color:#FFCB6B;">connection:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">26</span></span>
<span class="line"><span style="color:#FFCB6B;">uri:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/getVar</span></span>
<span class="line"><span style="color:#FFCB6B;">request_uri:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/getVar</span></span>
<span class="line"><span style="color:#FFCB6B;">scheme:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">http</span></span>
<span class="line"><span style="color:#FFCB6B;">request_method:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">GET</span></span>
<span class="line"><span style="color:#FFCB6B;">request_length:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">478</span></span>
<span class="line"><span style="color:#FFCB6B;">args:</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#FFCB6B;">arg_pid:</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#FFCB6B;">is_args:</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#FFCB6B;">query_string:</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#FFCB6B;">host:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">192.168</span><span style="color:#C3E88D;">.22.130</span></span>
<span class="line"><span style="color:#FFCB6B;">http_user_agent:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">Mozilla/5.0</span><span style="color:#A6ACCD;"> (Windows </span><span style="color:#C3E88D;">NT</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10.0</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">Win64</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">x64</span><span style="color:#A6ACCD;">) AppleWebKit/537.36 </span><span style="color:#89DDFF;">(</span><span style="color:#FFCB6B;">KHTML,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">like</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">Gecko</span><span style="color:#89DDFF;">)</span><span style="color:#A6ACCD;"> Chrome/108.0.0.0 Safari/537.36</span></span>
<span class="line"><span style="color:#FFCB6B;">http_referer:</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#FFCB6B;">http_via:</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#FFCB6B;">request_time:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0.000</span></span>
<span class="line"><span style="color:#FFCB6B;">https:</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#FFCB6B;">request_filename:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/nginx/html/getVar</span></span>
<span class="line"><span style="color:#FFCB6B;">document_root:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/nginx/html</span></span></code></pre></div><h3 id="日志记录全局变量" tabindex="-1">日志记录全局变量 <a class="header-anchor" href="#日志记录全局变量" aria-label="Permalink to &quot;日志记录全局变量&quot;">​</a></h3><p>上述参数还可以在日志文件中使用，这个就要用到前面我们介绍的<code>log_format</code>指令</p><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 定义在http块中，起名为main</span></span>
<span class="line"><span style="color:#C3E88D;">log_format main &#39;$remote_addr - $request - $status-$request_uri  $http_user_agent&#39;;</span></span>
<span class="line"><span style="color:#C3E88D;">server {</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">listen  8081;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">location /server {</span></span>
<span class="line"><span style="color:#89DDFF;">         </span><span style="color:#676E95;font-style:italic;"># 定义在server块中，main对应上面起的名字</span></span>
<span class="line"><span style="color:#89DDFF;">         </span><span style="color:#676E95;font-style:italic;"># 日志记录在logs下的access.log中</span></span>
<span class="line"><span style="color:#A6ACCD;">         </span><span style="color:#C3E88D;">access_log logs/access.log main;</span></span>
<span class="line"><span style="color:#89DDFF;">         </span><span style="color:#676E95;font-style:italic;"># 返回给页面看的，可以不写</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">default_type text/plain;</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">return 200 $args=$http_user_agent=$remote_addr;</span></span>
<span class="line"><span style="color:#A6ACCD;">	}</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><p>新日志的样子</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204291502302.png" alt="image-20220429150228214" style="zoom:80%;"><h2 id="set指令" tabindex="-1">set指令 <a class="header-anchor" href="#set指令" aria-label="Permalink to &quot;set指令&quot;">​</a></h2><p>该指令用来设置一个新的变量。</p><table><thead><tr><th>语法</th><th>set $variable value;</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>server、location、if</td></tr></tbody></table><p>variable:变量的名称，该变量名称要用&quot;$&quot;作为变量的第一个字符，且不要与Nginx服务器预设的全局变量同名。</p><p>value:变量的值，可以是字符串、其他变量或者变量的组合等。</p><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C3E88D;">server {</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">listen  8081;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">location /server {</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">set $name TOM;</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">set $age 18;</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">default_type text/plain;</span></span>
<span class="line"><span style="color:#A6ACCD;">  		</span><span style="color:#C3E88D;">return 200 $name=$age=$args;</span></span>
<span class="line"><span style="color:#A6ACCD;">	}</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><h2 id="if指令" tabindex="-1">if指令 <a class="header-anchor" href="#if指令" aria-label="Permalink to &quot;if指令&quot;">​</a></h2><p>该指令用来支持条件判断，并根据条件判断结果选择不同的Nginx配置。</p><table><thead css-module="."><tr><th>语法</th><th>if (condition)</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>server、location</td></tr></tbody></table><p>condition为判定条件，可以支持以下写法：</p><h3 id="条件是变量名" tabindex="-1">条件是变量名 <a class="header-anchor" href="#条件是变量名" aria-label="Permalink to &quot;条件是变量名&quot;">​</a></h3><p>变量名。如果变量名对应的值为空字符串或&quot;0&quot;，if都判断为false,其他条件为true。</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">if ($param){</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C3E88D;">location /testif {</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">set $username &#39;&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">default_type text/plain;</span></span>
<span class="line"><span style="color:#89DDFF;">   </span><span style="color:#676E95;font-style:italic;"># 使用变量名进行判断，变量名为空或0为false，if后要有空格</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">if ($username) {</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#C3E88D;">return 200 $username;</span></span>
<span class="line"><span style="color:#A6ACCD;">   }</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">return 200 &#39;param is empty&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><h3 id="条件是-和" tabindex="-1">条件是=和!= <a class="header-anchor" href="#条件是-和" aria-label="Permalink to &quot;条件是=和!=&quot;">​</a></h3><p>使用&quot;=&quot;和&quot;!=&quot;比较变量和字符串是否相等，满足条件为true，不满足为false</p><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C3E88D;">if ($request_method = POST){</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#C3E88D;">return 405;</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><p>注意：此处和Java不太一样的地方是字符串`不需要添加引号,并且等号和不等号前后到需要加空格。</p><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C3E88D;">location /testif {</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">default_type text/html;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">if ($args) {</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#C3E88D;">return 200 $args;</span></span>
<span class="line"><span style="color:#A6ACCD;">   }</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">if ($request_method = GET) {</span></span>
<span class="line"><span style="color:#89DDFF;">      </span><span style="color:#676E95;font-style:italic;"># 注意：返回中文会乱码，需要在server模块加上charset utf-8;</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#C3E88D;">return 405 &#39;页面访问为GET请求&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;">   }</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">return 200 &#39;param is empty&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><h3 id="正则" tabindex="-1">正则 <a class="header-anchor" href="#正则" aria-label="Permalink to &quot;正则&quot;">​</a></h3><p>使用正则表达式对变量进行匹配，匹配成功返回true，否则返回false。变量与正则表达式之间使用&quot;~&quot;,&quot;~*&quot;,&quot;!~&quot;,&quot;!~*&quot;</p><p>&quot;~&quot;代表匹配正则表达式过程中<code>区分大小写</code>，</p><p>&quot;~*&quot;代表匹配正则表达式过程中<code>不区分大小写</code></p><p>&quot;!~&quot;和&quot;!~*&quot;刚好<code>和上面取相反值</code>，如果匹配上返回false,匹配不上返回true</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">if </span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">$http_user_agent </span><span style="color:#89DDFF;">~</span><span style="color:#A6ACCD;"> MSIE</span><span style="color:#89DDFF;">){</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">$http_user_agent的值中是否包含MSIE字符串，如果包含返回true</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>注意：正则表达式字符串一般不需要加引号，但是如果字符串中包含&quot;}&quot;或者是&quot;;&quot;等字符时，就需要把引号加上。</p><p>示例</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">testif </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">  	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">  	</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 判断是否传入参数</span></span>
<span class="line"><span style="color:#A6ACCD;">	if ($http_user_agent </span><span style="color:#89DDFF;">~*</span><span style="color:#A6ACCD;"> Chrome) </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> Chrome</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">  	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> error</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p><a href="http://192.168.22.129:8081/testif" target="_blank" rel="noreferrer">http://192.168.22.129:8081/testif</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204291525899.png" alt="image-20220429152535814" style="zoom:67%;"><h3 id="文件-目录是否存在" tabindex="-1">文件&amp;目录是否存在 <a class="header-anchor" href="#文件-目录是否存在" aria-label="Permalink to &quot;文件&amp;目录是否存在&quot;">​</a></h3><blockquote><p>判断请求的文件是否存在使用&quot;-f&quot;和&quot;!-f&quot;,</p><p>判断请求的目录是否存在使用&quot;-d&quot;和&quot;!-d&quot;</p><p>判断请求的目录或者文件是否存在使用&quot;-e&quot;和&quot;!-e&quot;</p><p>判断请求的文件是否可执行使用&quot;-x&quot;和&quot;!-x&quot;</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">if </span><span style="color:#89DDFF;">(-</span><span style="color:#A6ACCD;">f $request_filename</span><span style="color:#89DDFF;">){</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">判断请求的文件是否存在</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">if </span><span style="color:#89DDFF;">(!-</span><span style="color:#A6ACCD;">f $request_filename</span><span style="color:#89DDFF;">){</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">判断请求的文件是否不存在</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>如果访问文件不存在，生成页面，这样不太好，进行改进</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204291531679.png" alt="image-20220429153104588" style="zoom:50%;"><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	root html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">  	if (</span><span style="color:#89DDFF;">!-</span><span style="color:#A6ACCD;">f $request_filename)</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	   </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">判断请求的文件是否存在</span></span>
<span class="line"><span style="color:#A6ACCD;">	   return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">&lt;h1&gt;file not found&lt;/h1&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;">	</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204291536072.png" alt="image-20220429153652986" style="zoom:50%;"><h3 id="适配-pc-或移动设备" tabindex="-1">适配 PC 或移动设备 <a class="header-anchor" href="#适配-pc-或移动设备" aria-label="Permalink to &quot;适配 PC 或移动设备&quot;">​</a></h3><p>根据用户设备不同返回不同样式的站点，以前经常使用的是纯前端的自适应布局，但无论是复杂性和易用性上面还是不如分开编写的好，比如我们常见的淘宝、京东......这些大型网站就都没有采用自适应，而是用分开制作的方式，根据用户请求的 <code>user-agent</code> 来判断是返回 PC 还是 H5 站点。</p><p>首先在 <code>/usr/share/nginx/html</code> 文件夹下 <code>mkdir</code> 分别新建两个文件夹 <code>PC</code> 和 <code>mobile</code>，<code>vim</code> 编辑两个 <code>index.html</code> 随便写点内容。</p><div class="language-bash"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/share/nginx/html</span></span>
<span class="line"><span style="color:#FFCB6B;">mkdir</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">pc</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">mobile</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">pc</span></span>
<span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">index.html</span><span style="color:#A6ACCD;">   </span><span style="color:#676E95;font-style:italic;"># 随便写点比如 hello pc!</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">../mobile</span></span>
<span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">index.html</span><span style="color:#A6ACCD;">   </span><span style="color:#676E95;font-style:italic;"># 随便写点比如 hello mobile!</span></span></code></pre></div><p>然后和设置二级域名虚拟主机时候一样，去 <code>/etc/nginx/conf.d</code> 文件夹下新建一个配置文件 <code>fe.sherlocked93.club.conf</code> ：</p><div class="language-nginx"><button title="Copy Code" class="copy"></button><span class="lang">nginx</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># /etc/nginx/conf.d/fe.sherlocked93.club.conf</span></span>
<span class="line"><span style="color:#C792EA;">server</span><span style="color:#A6ACCD;"> {</span></span>
<span class="line"><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;"> listen </span><span style="color:#A6ACCD;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">	server_name </span><span style="color:#A6ACCD;">fe.sherlocked93.club</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#C792EA;">location</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">/ </span><span style="color:#A6ACCD;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">	root </span><span style="color:#A6ACCD;"> /usr/share/nginx/html/pc</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">$</span><span style="color:#A6ACCD;">http_user_agent </span><span style="color:#89DDFF;">~* </span><span style="color:#C3E88D;">&#39;(Android|webOS|iPhone|iPod|BlackBerry)&#39;</span><span style="color:#A6ACCD;">) </span><span style="color:#F07178;">{</span></span>
<span class="line"><span style="color:#F07178;">       </span><span style="color:#89DDFF;"> root </span><span style="color:#F07178;">/usr/share/nginx/html/mobile</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#F07178;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">	index </span><span style="color:#A6ACCD;">index.html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	}</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><p>配置基本没什么不一样的，主要多了一个 <code>if</code> 语句，然后使用 <code>$http_user_agent</code> 全局变量来判断用户请求的 <code>user-agent</code>，指向不同的 root 路径，返回对应站点。</p><p>在浏览器访问这个站点，然后 F12 中模拟使用手机访问：</p><img src="https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2020/4/29/171c4e97062c5124~tplv-t2oaga2asx-zoom-in-crop-mark:1304:0:0:0.awebp" alt="62haogU3DtwMRiZ" style="zoom:50%;"><p>可以看到在模拟使用移动端访问的时候，Nginx 返回的站点变成了移动端对应的 html 了。</p><h2 id="break指令" tabindex="-1">break指令 <a class="header-anchor" href="#break指令" aria-label="Permalink to &quot;break指令&quot;">​</a></h2><p>该指令用于中断当前相同作用域中的其他Nginx配置。与该指令处于同一作用域的Nginx配置中，位于它前面的指令配置生效，位于后面的指令配置无效。并且break还有另外一个功能就是终止当前的匹配并把当前的URI在本location进行重定向访问处理。</p><table><thead><tr><th>语法</th><th>break;</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>server、location、if</td></tr></tbody></table><p>例子:需要在html下创建testbreak目录，在该目录下创建index.html，最后执行会跳转到该页面</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">location</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/testbreak</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#FFCB6B;">default_type</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">text/plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#82AAFF;">set</span><span style="color:#A6ACCD;"> $username </span><span style="color:#C3E88D;">TOM</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">$args</span><span style="color:#89DDFF;">)</span><span style="color:#A6ACCD;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		</span><span style="color:#82AAFF;">set</span><span style="color:#A6ACCD;"> $username </span><span style="color:#C3E88D;">JERRY</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">         </span><span style="color:#89DDFF;font-style:italic;">break</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		</span><span style="color:#82AAFF;">set</span><span style="color:#A6ACCD;"> $username </span><span style="color:#C3E88D;">ROSE</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	}</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#FFCB6B;">add_header</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">username</span><span style="color:#A6ACCD;"> $username</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> $username</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><h2 id="return指令-⭐" tabindex="-1">return指令 ⭐ <a class="header-anchor" href="#return指令-⭐" aria-label="Permalink to &quot;return指令 ⭐&quot;">​</a></h2><blockquote><p>返回http状态码 和 可选的第二个参数可以是重定向的URL，该指令用于完成对请求的处理，直接向客户端返回。在return后的所有Nginx配置都是无效的。</p></blockquote><h3 id="return-基本语法" tabindex="-1">return 基本语法 <a class="header-anchor" href="#return-基本语法" aria-label="Permalink to &quot;return 基本语法&quot;">​</a></h3><table><thead><tr><th>语法</th><th>return code [text]; return code URL; return URL;</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>server、location、if</td></tr></tbody></table><blockquote><p>code:为返回给客户端的HTTP状态代理。可以返回的状态代码为0~999的任意HTTP状态代理</p><p>text:为返回给客户端的响应体内容，支持变量的使用</p><p>URL:为返回给客户端的URL地址</p></blockquote><h3 id="return-小案例" tabindex="-1">return 小案例 <a class="header-anchor" href="#return-小案例" aria-label="Permalink to &quot;return 小案例&quot;">​</a></h3><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C3E88D;">location /testreturn {</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">default_type text/html;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">charset utf-8;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">return 200 &#39;&lt;h1&gt;成功&lt;/h1&gt;&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><p><a href="http://192.168.22.130:8081/testreturn" target="_blank" rel="noreferrer">http://192.168.22.130:8081/testreturn</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206231511116.png" alt="image-20220623151131972" style="zoom:50%;"><h3 id="直接返回状态码" tabindex="-1">直接返回状态码 <a class="header-anchor" href="#直接返回状态码" aria-label="Permalink to &quot;直接返回状态码&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">code </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   return </span><span style="color:#F78C6C;">200</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 直接返回状态码</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="返回状态码-一段文本" tabindex="-1">返回状态码 + 一段文本 <a class="header-anchor" href="#返回状态码-一段文本" aria-label="Permalink to &quot;返回状态码 + 一段文本&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">codeText </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   charset utf</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">8</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">访问成功</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 返回状态码 </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> 一段文本</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="返回状态码-重定向地址" tabindex="-1">返回状态码 + 重定向地址 <a class="header-anchor" href="#返回状态码-重定向地址" aria-label="Permalink to &quot;返回状态码 + 重定向地址&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">codeUrl </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   return </span><span style="color:#F78C6C;">302</span><span style="color:#A6ACCD;"> https:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">baidu</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 返回状态码 </span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;"> 重定向地址</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="返回重定向地址" tabindex="-1">返回重定向地址 <a class="header-anchor" href="#返回重定向地址" aria-label="Permalink to &quot;返回重定向地址&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">url </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   return https:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">baidu</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com </span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 返回重定向地址</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h1 id="url重写⭐" tabindex="-1">URL重写⭐ <a class="header-anchor" href="#url重写⭐" aria-label="Permalink to &quot;URL重写⭐&quot;">​</a></h1><blockquote><p>把匹配成功的URI替换成rewrite匹配的URI</p></blockquote><p>该指令具有一个可选参数和两个必需参数。</p><ul><li>第一个(必需)参数是请求URI必须匹配的正则表达式。</li><li>第二个参数是用于替换匹配URI的URI。</li><li>可选的第三个参数是可以停止进一步重写指令的处理或发送重定向(代码301或302)的标志</li></ul><blockquote><p>Rewrite是Nginx服务器提供的一个重要基本功能，是Web服务器产品中几乎必备的功能。主要的作用是用来实现URL的重写。比如输入www.360buy.com会自动跳转到www.jd.com，这就是地址重写</p></blockquote><blockquote><p>注意:Nginx服务器的Rewrite功能的实现依赖于PCRE(正则表达式库)的支持，因此在编译安装Nginx服务器之前，需要安装PCRE库。Nginx使用的是ngx_http_rewrite_module模块来解析和处理Rewrite功能的相关配置。这个库默认就有，不用自己装</p></blockquote><h2 id="rewrite的指令-重点" tabindex="-1">rewrite的指令(重点) <a class="header-anchor" href="#rewrite的指令-重点" aria-label="Permalink to &quot;rewrite的指令(重点)&quot;">​</a></h2><h3 id="rewrite-语法" tabindex="-1">rewrite 语法 <a class="header-anchor" href="#rewrite-语法" aria-label="Permalink to &quot;rewrite 语法&quot;">​</a></h3><h4 id="指令详解" tabindex="-1">指令详解 <a class="header-anchor" href="#指令详解" aria-label="Permalink to &quot;指令详解&quot;">​</a></h4><p>该指令通过正则表达式的使用来改变URI。可以同时存在一个或者多个指令，按照顺序依次对URL进行匹配和处理</p><table><thead><tr><th>语法</th><th>rewrite regex replacement [flag];</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>server、location、if</td></tr></tbody></table><p>regex:用来匹配URI的正则表达式</p><blockquote><p>replacement:匹配成功后，用于替换URI中被截取内容的字符串。如果该字符串是以&quot;http://&quot;或者&quot;https://&quot;开头的，则不会继续向下对URI进行其他处理，而是直接返回重写后的URI给客户端。</p></blockquote><p>会把匹配成功的url换成对应的url进行返回</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 以</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">url开头的任意网站都会跳转到https:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">baidu</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">url</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">w</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">$ https:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">baidu</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 以</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">test开头都能匹配到</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">test，$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">表示对应匹配到括号里的内容</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 注意：第二个变量表示第一个变量里括号的内容 </span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">表示任意字符比如</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">testaa</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">asd</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">aa都会匹配到</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">test</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">(test)</span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">$ </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">(demo)</span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">$ </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">test</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> test_success</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">demo</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> demo_success</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>进行访问</p><p><a href="http://192.168.22.129:8081/rewrite/urlabc" target="_blank" rel="noreferrer">http://192.168.22.129:8081/rewrite/urlabc</a> ：会自动跳转到百度</p><p><a href="http://192.168.22.129:8081/rewrite/testsad" target="_blank" rel="noreferrer">http://192.168.22.129:8081/rewrite/testsad</a> ：会自动跳转到<a href="http://192.168.22.129:8081/test" target="_blank" rel="noreferrer">http://192.168.22.129:8081/test</a></p><p><a href="http://192.168.22.129:8081/rewrite/demoeqa" target="_blank" rel="noreferrer">http://192.168.22.129:8081/rewrite/demoeqa</a> ：会自动跳转到<a href="http://192.168.22.129:8081/demo" target="_blank" rel="noreferrer">http://192.168.22.129:8081/demo</a></p><h4 id="配置项" tabindex="-1">配置项 <a class="header-anchor" href="#配置项" aria-label="Permalink to &quot;配置项&quot;">​</a></h4><p>flag:用来设置rewrite对URI的处理行为，可选值有如下：</p><h5 id="last-重点" tabindex="-1">last(重点) <a class="header-anchor" href="#last-重点" aria-label="Permalink to &quot;last(重点)&quot;">​</a></h5><blockquote><p>last重写后的 URL发起新请求，再次进入 server段，重试 location的中的匹配，为重写后的URI提供了转入到其他location块的机会。</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">(test)</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">w</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">$ </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> last</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">(demo)</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">w</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">$ </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> last</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">test</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> test_success</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">demo</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> demo_success</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>访问 <code>http://192.168.200.133:8081/rewrite/testabc</code>,能正确访问/test</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282207314.png" alt="1589475653252"></p><h5 id="break" tabindex="-1">break <a class="header-anchor" href="#break" aria-label="Permalink to &quot;break&quot;">​</a></h5><blockquote><p>直接使用重写后的 <code>URL</code> ，不再匹配其它 <code>location</code> 中语句；</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#/</span><span style="color:#A6ACCD;">test   </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">nginx</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">test</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">(test)</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">w</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">$ </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> break</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">(demo)</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">w</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">$ </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> break</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">test</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> test_success</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">demo</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> demo_success</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>访问 <code>http://192.168.200.133:8081/rewrite/demoabc</code>,页面报404错误</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282207316.png" alt="1589475732042"></p><h5 id="redirct-重定向" tabindex="-1">redirct 重定向 <a class="header-anchor" href="#redirct-重定向" aria-label="Permalink to &quot;redirct 重定向&quot;">​</a></h5><blockquote><p>redirect：将重写后的URI返回给客户端，状态码为302，指明是临时重定向URI,主要用在replacement变量不是以&quot;http://&quot;或者&quot;https://&quot;开头的情况</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location rewrite </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">(test)</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">w</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">$ </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> redirect</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">(demo)</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">w</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">$ </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> redirect</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">test</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> test_success</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">demo</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> demo_success</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>访问<code>http://192.168.200.133:8081/rewrite/testabc</code>请求会被临时重定向，浏览器地址也会发生改变</p><h5 id="permanent-永久重定向" tabindex="-1">permanent 永久重定向 <a class="header-anchor" href="#permanent-永久重定向" aria-label="Permalink to &quot;permanent 永久重定向&quot;">​</a></h5><blockquote><p>permanent：将重写后的URI返回给客户端，状态码为301，指明是永久重定向URI,主要用在replacement变量不是以&quot;http://&quot;或者&quot;https://&quot;开头的情况。</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location rewrite </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">(test)</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">w</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">$ </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> permanent</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">(demo)</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">w</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">$ </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> permanent</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">test</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> test_success</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">demo</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> demo_success</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>访问<code>http://192.168.200.133:8081/rewrite/testabc</code>请求会被永久重定向，<code>浏览器地址也会发生改变</code></p><h3 id="rewrite-log指令" tabindex="-1">rewrite_log指令 <a class="header-anchor" href="#rewrite-log指令" aria-label="Permalink to &quot;rewrite_log指令&quot;">​</a></h3><blockquote><p>开启后，URL重写的相关日志将以notice级别输出到error_log指令配置的日志文件汇总。</p></blockquote><table><thead><tr><th>语法</th><th>rewrite_log on|off;</th></tr></thead><tbody><tr><td>默认值</td><td>rewrite_log off;</td></tr><tr><td>位置</td><td>http、server、location、if</td></tr></tbody></table><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite_log on</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	error_log  logs</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">error</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">log notice</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">     </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">以</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">url开头的任意网站都会跳转到https:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">baidu</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">rewrite</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">url</span><span style="color:#89DDFF;">\</span><span style="color:#A6ACCD;">w</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;">$ https:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">baidu</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204291623245.png" alt="image-20220429162324148" style="zoom:80%;"><h2 id="域名跳转" tabindex="-1">域名跳转 <a class="header-anchor" href="#域名跳转" aria-label="Permalink to &quot;域名跳转&quot;">​</a></h2><h3 id="问题分析" tabindex="-1">问题分析 <a class="header-anchor" href="#问题分析" aria-label="Permalink to &quot;问题分析&quot;">​</a></h3><p>先来看一个效果，如果我们想访问京东网站，大家都知道我们可以输入<code>www.jd.com</code>,但是同样的我们也可以输入<code>www.360buy.com</code>同样也都能访问到京东网站。这个其实是因为京东刚开始的时候域名就是www.360buy.com，后面由于各种原因把自己的域名换成了www.jd.com,</p><p>虽然说域名变量，但是对于以前只记住了www.360buy.com的用户来说，我们如何把这部分用户也迁移到我们新域名的访问上来，针对于这个问题，我们就可以使用Nginx中Rewrite的域名跳转来解决。</p><p>目的：无论访问这三个域名中的哪一个域名，最终都会跳转到www.itcast.cn这个域名</p><h3 id="环境准备" tabindex="-1">环境准备 <a class="header-anchor" href="#环境准备" aria-label="Permalink to &quot;环境准备&quot;">​</a></h3><p>准备三个域名</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vim </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">etc</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">hosts</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">127.0.0.1</span><span style="color:#A6ACCD;">   www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span></span>
<span class="line"><span style="color:#F78C6C;">127.0.0.1</span><span style="color:#A6ACCD;">   www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span></span>
<span class="line"><span style="color:#F78C6C;">127.0.0.1</span><span style="color:#A6ACCD;">   www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com</span></span></code></pre></div><p>通过Nginx实现访问www.itcast.cn</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">&lt;h1&gt;welcome to itcast&lt;/h1&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="基础实现" tabindex="-1">基础实现 <a class="header-anchor" href="#基础实现" aria-label="Permalink to &quot;基础实现&quot;">​</a></h3><p>通过Rewrite完成将www.ithema.com和www.itheima.cn的请求跳转到www.itcast.com</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">以</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">开头都会跳转到http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="进阶实现" tabindex="-1">进阶实现 <a class="header-anchor" href="#进阶实现" aria-label="Permalink to &quot;进阶实现&quot;">​</a></h3><p>问题描述:如何在域名跳转的过程中携带请求的URI？修改配置信息</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">)能匹配到</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">getUser等指令</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^(</span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">) http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">；</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="完整实现" tabindex="-1">完整实现 <a class="header-anchor" href="#完整实现" aria-label="Permalink to &quot;完整实现&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">&lt;h1&gt;welcome to itcast&lt;/h1&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> (</span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">)能匹配到</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">getUser等指令</span></span>
<span class="line"><span style="color:#A6ACCD;">    rewrite ^(</span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">) http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">；</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>www.itheima.cn/getUser跳转到<a href="http://www.itcast.cn/getUser" target="_blank" rel="noreferrer">http://www.itcast.cn/getUser</a></p><p>www.itheima.cn/getUser/getOne跳转到<a href="http://www.itcast.cn/getUser/getOne" target="_blank" rel="noreferrer">http://www.itcast.cn/getUser/getOne</a></p><h2 id="域名镜像" tabindex="-1">域名镜像 <a class="header-anchor" href="#域名镜像" aria-label="Permalink to &quot;域名镜像&quot;">​</a></h2><h3 id="需求描述" tabindex="-1">需求描述 <a class="header-anchor" href="#需求描述" aria-label="Permalink to &quot;需求描述&quot;">​</a></h3><blockquote><p>镜像网站指定是将一个完全相同的网站分别放置到几台服务器上，并分别使用独立的URL进行访问。其中一台服务器上的网站叫主站，其他的为镜像网站。镜像网站和主站没有太大的区别，可以把镜像网站理解为主站的一个备份节点。可以通过镜像网站提供网站在不同地区的响应速度。镜像网站可以平衡网站的流量负载、可以解决网络宽带限制、封锁等。</p></blockquote><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282207319.png" alt="1589560433192"></p><p>而我们所说的域名镜像和网站镜像比较类似，上述案例中，将www.itheima.com和 www.itheima.cn都能跳转到www.itcast.cn，那么www.itcast.cn我们就可以把它起名叫主域名，其他两个就是我们所说的镜像域名，当然如果我们不想把整个网站做镜像，只想为其中某一个子目录下的资源做镜像，我们可以在location块中配置rewrite功能，比如:</p><h3 id="完整实现-1" tabindex="-1">完整实现 <a class="header-anchor" href="#完整实现-1" aria-label="Permalink to &quot;完整实现&quot;">​</a></h3><p>我只想要user模块进行跳转，其他模块不去跳转</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen          </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name     www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">user </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">         </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 以</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">user开头的所有请求，都要跳转到http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span></span>
<span class="line"><span style="color:#A6ACCD;">    	rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">user(</span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">)$ http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn$</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 访问emp则不会进行域名跳转</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">emp</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">&lt;h1&gt;emp_success&lt;/h1&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h2 id="独立域名" tabindex="-1">独立域名 <a class="header-anchor" href="#独立域名" aria-label="Permalink to &quot;独立域名&quot;">​</a></h2><blockquote><p>一个完整的项目包含多个模块，比如购物网站有商品搜索模块、商品详情模块和购物车模块等，那么我们如何为每一个模块设置独立的域名。</p></blockquote><h3 id="需求描述-1" tabindex="-1">需求描述 <a class="header-anchor" href="#需求描述-1" aria-label="Permalink to &quot;需求描述&quot;">​</a></h3><p>网络上的域名ip不一致，可以都使用80，而目前使用的是本机配置的域名，则要用端口号区分</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">http</span><span style="color:#89DDFF;">://</span><span style="color:#A6ACCD;">search</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com:</span><span style="color:#F78C6C;">81</span><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 访问商品搜索模块</span></span>
<span class="line"><span style="color:#A6ACCD;">http</span><span style="color:#89DDFF;">://</span><span style="color:#A6ACCD;">item</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com:</span><span style="color:#F78C6C;">82</span><span style="color:#A6ACCD;">	  </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 访问商品详情模块</span></span>
<span class="line"><span style="color:#A6ACCD;">http</span><span style="color:#89DDFF;">://</span><span style="color:#A6ACCD;">cart</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com:</span><span style="color:#F78C6C;">83</span><span style="color:#A6ACCD;">	  </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 访问商品购物车模块</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">81</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name search</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 最终访问:http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">search</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">xxx</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^(</span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">) http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">search$</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">82</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name item</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^(</span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">) http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">item$</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">83</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name cart</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	rewrite ^(</span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">) http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">cart$</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h2 id="目录自动添加-新版本不考虑" tabindex="-1">目录自动添加&quot;/&quot;(新版本不考虑) <a class="header-anchor" href="#目录自动添加-新版本不考虑" aria-label="Permalink to &quot;目录自动添加&quot;/&quot;(新版本不考虑)&quot;">​</a></h2><h3 id="问题描述" tabindex="-1">问题描述 <a class="header-anchor" href="#问题描述" aria-label="Permalink to &quot;问题描述&quot;">​</a></h3><p>通过一个例子来演示下问题:</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen	</span><span style="color:#F78C6C;">8082</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">heima </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		root html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		index index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>通过<code>http://192.168.200.133:8082/heima</code>和通过<code>http://192.168.200.133:8082/heima/</code>访问的区别？</p><p>如果不加斜杠，Nginx服务器内部会自动做一个301的重定向，重定向的地址会有一个指令叫server_name_in_redirect on|off;来决定重定向的地址：</p><blockquote><p>如果该指令为on，重定向的地址为: http://server_name:8082/目录名/; :8082/heima/ 如果该指令为off，重定向的地址为: <a href="http://xn--URL-x68dw2w8sa37sez2e:8082/%E7%9B%AE%E5%BD%95%E5%90%8D/" target="_blank" rel="noreferrer">http://原URL中的域名:8082/目录名/</a>; <a href="http://192.168.200.133:8082/heima/" target="_blank" rel="noreferrer">http://192.168.200.133:8082/heima/</a></p></blockquote><p>所以就拿刚才的地址来说，<a href="http://192.168.200.133:8082/heima%E5%A6%82%E6%9E%9C%E4%B8%8D%E5%8A%A0%E6%96%9C%E6%9D%A0%EF%BC%8C%E9%82%A3%E4%B9%88%E6%8C%89%E7%85%A7%E4%B8%8A%E8%BF%B0%E8%A7%84%E5%88%99%EF%BC%8C%E5%A6%82%E6%9E%9C%E6%8C%87%E4%BB%A4server_name_in_redirect%E4%B8%BAon%EF%BC%8C%E5%88%99301%E9%87%8D%E5%AE%9A%E5%90%91%E5%9C%B0%E5%9D%80%E5%8F%98%E4%B8%BA" target="_blank" rel="noreferrer">http://192.168.200.133:8082/heima如果不加斜杠，那么按照上述规则，如果指令server_name_in_redirect为on，则301重定向地址变为</a> :8082/heima/,如果为off，则301重定向地址变为<a href="http://192.168.200.133:8082/heima/%E3%80%82%E5%90%8E%E9%9D%A2%E8%BF%99%E4%B8%AA%E6%98%AF%E6%AD%A3%E5%B8%B8%E7%9A%84%EF%BC%8C%E5%89%8D%E9%9D%A2%E5%9C%B0%E5%9D%80%E5%B0%B1%E6%9C%89%E9%97%AE%E9%A2%98%E3%80%82" target="_blank" rel="noreferrer">http://192.168.200.133:8082/heima/。后面这个是正常的，前面地址就有问题。</a></p><blockquote><p>注意server_name_in_redirect指令在Nginx的0.8.48版本之前默认都是on，之后改成了off,所以现在我们这个版本不需要考虑这个问题，但是如果是0.8.48以前的版本并且server_name_in_redirect设置为on，我们如何通过rewrite来解决这个问题？</p></blockquote><h3 id="解决方案-1" tabindex="-1">解决方案 <a class="header-anchor" href="#解决方案-1" aria-label="Permalink to &quot;解决方案&quot;">​</a></h3><p>我们可以使用rewrite功能为末尾没有斜杠的URL自动添加一个斜杠</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen	</span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name_in_redirect on</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">heima </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		if (</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">d $request_filename)</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">			rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">(</span><span style="color:#89DDFF;">.*</span><span style="color:#A6ACCD;">)([^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">])$ http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">$host</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> permanent</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h2 id="合并目录" tabindex="-1">合并目录 <a class="header-anchor" href="#合并目录" aria-label="Permalink to &quot;合并目录&quot;">​</a></h2><blockquote><p>搜索引擎优化(SEO)是一种利用搜索引擎的搜索规则来提高目的网站在有关搜索引擎内排名的方式。我们在创建自己的站点时，可以通过很多中方式来有效的提供搜索引擎优化的程度。</p></blockquote><blockquote><p>其中有一项就包含URL的目录层级一般不要超过三层，否则的话不利于搜索引擎的搜索也给客户端的输入带来了负担，但是将所有的文件放在一个目录下又会导致文件资源管理混乱并且访问文件的速度也会随着文件增多而慢下来，这两个问题是相互矛盾的，那么使用rewrite如何解决上述问题?</p></blockquote><p>举例，网站中有一个资源文件的访问路径时 /server/11/22/33/44/20.html,也就是说20.html存在于第5级目录下，如果想要访问该资源文件，客户端的URL地址就要写成 <code>http://192.168.200.133/server/11/22/33/44/20.html</code>,</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		root html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>但是这个是非常不利于SEO搜索引擎优化的，同时客户端也不好记.使用rewrite我们可以进行如下配置:</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	     </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> $表示结束标志，之后要空格写</span></span>
<span class="line"><span style="color:#A6ACCD;">		rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">([</span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">9</span><span style="color:#A6ACCD;">]</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">)</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">([</span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">9</span><span style="color:#A6ACCD;">]</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">)</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">([</span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">9</span><span style="color:#A6ACCD;">]</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">)</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">([</span><span style="color:#F78C6C;">0</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">9</span><span style="color:#A6ACCD;">]</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">)</span><span style="color:#89DDFF;">\.</span><span style="color:#A6ACCD;">html$ </span></span>
<span class="line"><span style="color:#A6ACCD;">		        </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">4</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">5</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html last</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><blockquote><p>这样，客户端只需要输入<a href="http://www.web.name/server-11-22-33-44-20.html%E5%B0%B1%E5%8F%AF%E4%BB%A5%E8%AE%BF%E9%97%AE%E5%88%B020.html%E9%A1%B5%E9%9D%A2%E4%BA%86%E3%80%82%E8%BF%99%E9%87%8C%E4%B9%9F%E5%85%85%E5%88%86%E5%88%A9%E7%94%A8%E4%BA%86rewrite%E6%8C%87%E4%BB%A4%E6%94%AF%E6%8C%81%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E7%89%B9%E6%80%A7%E3%80%82" target="_blank" rel="noreferrer">http://www.web.name/server-11-22-33-44-20.html就可以访问到20.html页面了。这里也充分利用了rewrite指令支持正则表达式的特性。</a></p></blockquote><h2 id="防盗链优化" tabindex="-1">防盗链优化 <a class="header-anchor" href="#防盗链优化" aria-label="Permalink to &quot;防盗链优化&quot;">​</a></h2><blockquote><p>防盗链之前我们已经介绍过了相关的知识，在rewrite中的防盗链和之前将的原理其实都是一样的，只不过通过rewrite可以将防盗链的功能进行完善下，当出现防盗链的情况，我们可以使用rewrite将请求转发到自定义的一张图片和页面，给用户比较好的提示信息。下面我们就通过根据文件类型实现防盗链的一个配置实例:</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">images </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    root html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 不满足如下三种情况，就是盗链</span></span>
<span class="line"><span style="color:#A6ACCD;">    valid_referers none blocked www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">baidu</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">com</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    if ($invalid_referer)</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">return </span><span style="color:#F78C6C;">403</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        rewrite ^</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">images</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">forbidden</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">png break</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h1 id="反向代理-重点" tabindex="-1">反向代理(重点) <a class="header-anchor" href="#反向代理-重点" aria-label="Permalink to &quot;反向代理(重点)&quot;">​</a></h1><h2 id="正向代理和反向代理⭐" tabindex="-1">正向代理和反向代理⭐ <a class="header-anchor" href="#正向代理和反向代理⭐" aria-label="Permalink to &quot;正向代理和反向代理⭐&quot;">​</a></h2><p>反向代理（Reverse Proxy）对应的是正向代理（Forward Proxy），他们的区别：</p><blockquote><p><strong>正向代理：</strong> 一般的访问流程是客户端直接向目标服务器发送请求并获取内容，使用正向代理后，客户端改为向代理服务器发送请求，并指定目标服务器（原始服务器），然后由代理服务器和原始服务器通信，转交请求并获得的内容，再返回给客户端。<strong>正向代理隐藏了真实的客户端，为客户端收发请求，使真实客户端对服务器不可见</strong>；</p></blockquote><blockquote><p>举个具体的例子 🌰，你的浏览器无法直接访问谷哥，这时候可以通过一个代理服务器来帮助你访问谷哥，那么这个服务器就叫正向代理。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5.13/202205221147374.png" alt="image-20220522114748286" style="zoom:67%;"><blockquote><p><strong>反向代理：</strong> 与一般访问流程相比，使用反向代理后，直接收到请求的服务器是代理服务器，然后将请求转发给内部网络上真正进行处理的服务器，得到的结果返回给客户端。反向代理<strong>隐藏了真实的服务器</strong>，为服务器收发请求，使真实服务器对客户端不可见。一般在处理跨域请求的时候比较常用。基本上所有的大型网站都设置了反向代理</p></blockquote><blockquote><p>举个具体的例子 🌰，去饭店吃饭，可以点川菜、粤菜、江浙菜，饭店也分别有三个菜系的厨师 👨‍🍳，但是你作为顾客不用管哪个厨师给你做的菜，只用点菜即可，小二将你菜单中的菜分配给不同的厨师来具体处理，那么这个小二就是反向代理服务器。</p></blockquote><blockquote><p>简单的说，一般给客户端做代理的都是正向代理，给服务器做代理的就是反向代理。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.5.13/202205221148020.png" alt="image-20220522114804931" style="zoom:67%;"><h2 id="正向代理实现" tabindex="-1">正向代理实现 <a class="header-anchor" href="#正向代理实现" aria-label="Permalink to &quot;正向代理实现&quot;">​</a></h2><blockquote><p>我们先来通过一个小案例演示下Nginx正向代理的简单应用。</p></blockquote><h3 id="需求描述-2" tabindex="-1">需求描述 <a class="header-anchor" href="#需求描述-2" aria-label="Permalink to &quot;需求描述&quot;">​</a></h3><p>需要准备3台服务器</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282207317.png" style="zoom:80%;"><h3 id="具体实现" tabindex="-1">具体实现 <a class="header-anchor" href="#具体实现" aria-label="Permalink to &quot;具体实现&quot;">​</a></h3><p>(1)服务端的设置</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">http </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    log_format main </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">client send request=&gt;clientIp=$remote_addr serverIp=&gt;$host</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		listen </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		server_name	localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		access_log logs</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">access</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">log main</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		location </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">			root html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">			index index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">htm</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>(2)使用客户端访问服务端，打开日志查看结果</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282207333.png" alt="1589729000713" style="zoom:67%;"><p>(3)代理服务器设置：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        listen  </span><span style="color:#F78C6C;">82</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        resolver </span><span style="color:#F78C6C;">8.8.8.8</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">                proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">$host$request_uri</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span></code></pre></div><p>(4)查看代理服务器的IP(192.168.200.146)和Nginx配置监听的端口(82)</p><p>(5)在客户端配置代理服务器</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282207339.png" alt=""></p><p>(6)设置完成后，再次通过浏览器访问服务端</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282207812.png" alt="1589729479920" style="zoom:67%;"><p>通过对比，上下两次的日志记录，会发现虽然我们是客户端访问服务端，但是如何使用了代理，那么服务端能看到的只是代理发送过去的请求，这样的化，就使用Nginx实现了正向代理的设置。但是Nginx正向代理，在实际的应用中不是特别多，所以我们简单了解下</p><h2 id="反向代理语法" tabindex="-1">反向代理语法 <a class="header-anchor" href="#反向代理语法" aria-label="Permalink to &quot;反向代理语法&quot;">​</a></h2><p>Nginx反向代理模块的指令是由<code>ngx_http_proxy_module</code>模块进行解析，该模块在安装Nginx的时候已经自己加装到Nginx中了，接下来我们把反向代理中的常用指令一一介绍下：</p><h3 id="proxy-pass-被代理地址-⭐" tabindex="-1">proxy_pass(被代理地址) ⭐ <a class="header-anchor" href="#proxy-pass-被代理地址-⭐" aria-label="Permalink to &quot;proxy_pass(被代理地址) ⭐&quot;">​</a></h3><p>该指令用来设置被代理服务器地，可以是主机名称、IP地址加端口号形式。</p><table><thead><tr><th>语法</th><th>proxy_pass URL;</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>location</td></tr></tbody></table><blockquote><ol><li><code>URL</code> 必须以 <code>http</code> 或 <code>https</code> 开头；</li><li><code>URL</code> 中可以携带变量；</li><li><code>URL</code> 中是否带 <code>URI</code> ，会直接影响发往上游请求的 <code>URL</code></li></ol></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">	    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 被代理服务器的地址 </span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#89DDFF;">/;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 当客户端访问 </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">效果是一样的</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 也就是location配置的路径不起作用了</span></span>
<span class="line"><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#89DDFF;">/;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><blockquote><p>当客户端访问 /server/index.html 这个时候，第一个没加/proxy_pass就变成了/server/index.html 第二个加上/后proxy_pass就变成了/index.html效果就不一样了。</p></blockquote><h3 id="proxy-set-header-更改请求头" tabindex="-1">proxy_set_header(更改请求头) <a class="header-anchor" href="#proxy-set-header-更改请求头" aria-label="Permalink to &quot;proxy_set_header(更改请求头)&quot;">​</a></h3><p>该指令可以更改Nginx服务器接收到的客户端请求的请求头信息，然后将新的请求头发送给代理的服务器</p><table><thead><tr><th>语法</th><th>proxy_set_header field value;</th></tr></thead><tbody><tr><td>默认值</td><td>proxy_set_header Host $proxy_host;<br>proxy_set_header Connection close;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><p>需要注意的是，如果想要看到结果，必须在被代理的服务器上来获取添加的头信息。</p><p>被代理服务器： [192.168.200.146]</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen  </span><span style="color:#F78C6C;">8080</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">plain</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> $http_username</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>代理服务器: [192.168.200.133]</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   listen  </span><span style="color:#F78C6C;">8080</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8080</span><span style="color:#89DDFF;">/;</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_set_header username TOM</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>访问测试：192.168.200.133:8080/server/ 即可看到页面显示TOM</p><h3 id="proxy-redirect" tabindex="-1">proxy_redirect <a class="header-anchor" href="#proxy-redirect" aria-label="Permalink to &quot;proxy_redirect&quot;">​</a></h3><blockquote><p>该指令是用来重置头信息中的&quot;Location&quot;和&quot;Refresh&quot;的值。避免直接显示被代理服务器的IP地址</p></blockquote><table><thead><tr><th>语法</th><th>proxy_redirect redirect replacement;<br>proxy_redirect default;<br>proxy_redirect off;</th></tr></thead><tbody><tr><td>默认值</td><td>proxy_redirect default;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><h4 id="指令选项" tabindex="-1">指令选项 <a class="header-anchor" href="#指令选项" aria-label="Permalink to &quot;指令选项&quot;">​</a></h4><p>proxy_redirect redirect replacement;</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">redirect</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;">目标</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">Location的值</span></span>
<span class="line"><span style="color:#A6ACCD;">replacement</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;">要替换的值</span></span></code></pre></div><p>proxy_redirect default;</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">default</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">将location块的uri变量作为replacement</span><span style="color:#89DDFF;">,</span></span>
<span class="line"><span style="color:#A6ACCD;">将proxy_pass变量作为redirect进行替换</span></span></code></pre></div><p>proxy_redirect off;</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">关闭proxy_redirect的功能</span></span></code></pre></div><h4 id="示例" tabindex="-1">示例 <a class="header-anchor" href="#示例" aria-label="Permalink to &quot;示例&quot;">​</a></h4><p>为什么要用该指令? 首先定义一个server</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen  </span><span style="color:#F78C6C;">8082</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        if (</span><span style="color:#89DDFF;">!-</span><span style="color:#A6ACCD;">f $request_filename)</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">          </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 请求的资源如果不存在，那么他就会进行跳转到该路径上，避免显示</span><span style="color:#F78C6C;">404</span></span>
<span class="line"><span style="color:#A6ACCD;">            return </span><span style="color:#F78C6C;">302</span><span style="color:#A6ACCD;"> http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.1.107</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8085</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">girl</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">jpg</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>访问测试：<a href="http://192.168.22.130:8082/ab.jpg" target="_blank" rel="noreferrer">http://192.168.22.130:8082/ab.jpg</a></p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">curl </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">I  http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8082</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">girl1</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">jpg</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211549419.png" alt="image-20220621154908298" style="zoom:67%;"><p>显示：注意上面路径已经变成了：<a href="http://192.168.1.107:8085/girl.jpg%EF%BC%8C%E5%A6%82%E4%BD%95%E4%BF%AE%E6%94%B9%E8%BF%99%E4%B8%AA%E8%B7%AF%E5%BE%84%E5%91%A2" target="_blank" rel="noreferrer">http://192.168.1.107:8085/girl.jpg，如何修改这个路径呢</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211520020.png" alt="image-20220621152005750" style="zoom:50%;"><p>再设置一个server，进行反向代理</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen  </span><span style="color:#F78C6C;">8085</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8082</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_redirect http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.1.107</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8085</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">girl</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">jpg http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8085</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">girl</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">jpg</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p><a href="http://192.168.22.130:8085/girl1.jpg%E4%BC%9A%E8%BD%AC%E6%8D%A2%E6%88%90http://192.168.22.130:8085/girl.jpg" target="_blank" rel="noreferrer">http://192.168.22.130:8085/girl1.jpg会转换成http://192.168.22.130:8085/girl.jpg</a></p><h3 id="proxy-pass-注意事项-⭐" tabindex="-1">proxy_pass 注意事项 ⭐ <a class="header-anchor" href="#proxy-pass-注意事项-⭐" aria-label="Permalink to &quot;proxy_pass 注意事项 ⭐&quot;">​</a></h3><blockquote><p>编写proxy_pass的时候，后面的值要不要加&quot;/&quot;? 如下</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.100.33</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8081</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.100.33</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8081</span><span style="color:#89DDFF;">/</span></span></code></pre></div><p>这两种用法的区别就是带 <code>/</code> 和不带 <code>/</code> ，在配置代理时它们的区别可大了：</p><ul><li>不带 <code>/</code> 意味着 <code>Nginx</code> 不会修改用户 <code>URL</code> ，而是直接透传给上游的应用服务器；</li><li>带 <code>/</code> 意味着 <code>Nginx</code> 会修改用户 <code>URL</code> ，修改方法是将 <code>location</code> 后的 <code>URL</code> 从用户 <code>URL</code> 中删除</li></ul><p>不带 <code>/</code> 的用法</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">bbs</span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">    proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">127.0.0.1</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8080</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><ol><li>用户请求 <code>URL</code> ： <code>/bbs/abc/test.html</code></li><li>请求到达 <code>Nginx</code> 的 <code>URL</code> ： <code>/bbs/abc/test.html</code></li><li>请求到达上游应用服务器的 <code>URL</code> ： <code>/bbs/abc/test.html</code></li></ol><p>带 <code>/</code> 的用法</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">bbs</span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">    proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">127.0.0.1</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8080</span><span style="color:#89DDFF;">/;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><ol><li>用户请求 <code>URL</code> ： <code>/bbs/abc/test.html</code></li><li>请求到达 <code>Nginx</code> 的 <code>URL</code> ： <code>/bbs/abc/test.html</code></li><li>请求到达上游应用服务器的 <code>URL</code> ： <code>/abc/test.html</code></li></ol><p>并没有拼接上 <code>/bbs</code> ，这点和 <code>root</code> 与 <code>alias</code> 之间的区别是保持一致的</p><h2 id="反向代理实战-⭐" tabindex="-1">反向代理实战 ⭐ <a class="header-anchor" href="#反向代理实战-⭐" aria-label="Permalink to &quot;反向代理实战 ⭐&quot;">​</a></h2><h3 id="需求描述-3" tabindex="-1">需求描述 <a class="header-anchor" href="#需求描述-3" aria-label="Permalink to &quot;需求描述&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282207821.png" alt="1581883378672" style="zoom:67%;"><p>服务器1,2,3存在两种情况</p><blockquote><p>第一种情况: 三台服务器的内容不一样。那么代理服务器要定向的访问服务器 第二种情况: 三台服务器的内容是一样。那么代理服务器要<strong>负载均衡</strong>的访问服务器</p></blockquote><p>以下演示三个服务器内容不同的情况</p><p>如果服务器1、服务器2和服务器3的内容不一样，那我们可以根据用户请求来分发到不同的服务器。</p><h3 id="代理服务器" tabindex="-1">代理服务器 <a class="header-anchor" href="#代理服务器" aria-label="Permalink to &quot;代理服务器&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen    </span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name     localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">&lt;h1&gt;192.168.22.130:9001&lt;/h1&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen    </span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name     localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">&lt;h1&gt;192.168.22.130:9002&lt;/h1&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen    </span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name     localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">&lt;h1&gt;192.168.22.130:9003&lt;/h1&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>进行访问：都能访问成功</p><p><a href="http://192.168.22.130:9001/" target="_blank" rel="noreferrer">http://192.168.22.130:9001/</a></p><p><a href="http://192.168.22.130:9002/" target="_blank" rel="noreferrer">http://192.168.22.130:9002/</a></p><p><a href="http://192.168.22.130:9003/" target="_blank" rel="noreferrer">http://192.168.22.130:9003/</a></p><h3 id="配置代理" tabindex="-1">配置代理 <a class="header-anchor" href="#配置代理" aria-label="Permalink to &quot;配置代理&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">     listen           </span><span style="color:#F78C6C;">8082</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">     server_name      localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">     location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server1 </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">         </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 末尾加</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">，这样</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server1就不会在访问时代理添加到服务器上了</span></span>
<span class="line"><span style="color:#A6ACCD;">         </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 原来http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">/</span></span>
<span class="line"><span style="color:#A6ACCD;">         proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">/;</span></span>
<span class="line"><span style="color:#A6ACCD;">     </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">     location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server2 </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">         proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">/;</span></span>
<span class="line"><span style="color:#A6ACCD;">     </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">     location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server3 </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">         proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">/;</span></span>
<span class="line"><span style="color:#A6ACCD;">     </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="访问测试" tabindex="-1">访问测试 <a class="header-anchor" href="#访问测试" aria-label="Permalink to &quot;访问测试&quot;">​</a></h3><p><a href="http://192.168.22.130:8082/server1%E4%BC%9A%E4%BB%A3%E7%90%86%E5%88%B0http://192.168.22.130:9001" target="_blank" rel="noreferrer">http://192.168.22.130:8082/server1会代理到http://192.168.22.130:9001</a></p><p><a href="http://192.168.22.130:8082/server2%E4%BC%9A%E4%BB%A3%E7%90%86%E5%88%B0http://192.168.22.130:9002" target="_blank" rel="noreferrer">http://192.168.22.130:8082/server2会代理到http://192.168.22.130:9002</a></p><p><a href="http://192.168.22.130:8082/server3%E4%BC%9A%E4%BB%A3%E7%90%86%E5%88%B0http://192.168.22.130:9003" target="_blank" rel="noreferrer">http://192.168.22.130:8082/server3会代理到http://192.168.22.130:9003</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211618079.png" alt="image-20220621161856953" style="zoom:67%;"><h2 id="缓冲区-反向代理优化" tabindex="-1">缓冲区+反向代理优化 <a class="header-anchor" href="#缓冲区-反向代理优化" aria-label="Permalink to &quot;缓冲区+反向代理优化&quot;">​</a></h2><h3 id="为什么需要缓冲区" tabindex="-1">为什么需要缓冲区 <a class="header-anchor" href="#为什么需要缓冲区" aria-label="Permalink to &quot;为什么需要缓冲区&quot;">​</a></h3><blockquote><p>先来思考一个问题，接入Nginx的项目一般请求流程为：“客户端→Nginx→服务端”，在这个过程中存在两个连接：“客户端→Nginx、Nginx→服务端”，那么两个不同的连接速度不一致，就会影响用户的体验（比如浏览器的加载速度跟不上服务端的响应速度）。</p></blockquote><blockquote><p>其实也就类似电脑的内存跟不上CPU速度，所以对于用户造成的体验感极差，因此在CPU设计时都会加入三级高速缓冲区，用于缓解CPU和内存速率不一致的矛盾。在Nginx也同样存在缓冲区的机制，主要目的就在于：<strong>「用来解决两个连接之间速度不匹配造成的问题」</strong> ，有了缓冲后，Nginx代理可暂存后端的响应，然后按需供给数据给客户端。先来看看一些关于缓冲区的配置项：</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282207828.png" alt="1581879638569" style="zoom:67%;"><h3 id="proxy-buffer相关指令" tabindex="-1">Proxy Buffer相关指令 <a class="header-anchor" href="#proxy-buffer相关指令" aria-label="Permalink to &quot;Proxy Buffer相关指令&quot;">​</a></h3><blockquote><p>proxy_buffering :该指令用来开启或者关闭代理服务器的缓冲区；</p></blockquote><table><thead><tr><th>语法</th><th>proxy_buffering on|off;</th></tr></thead><tbody><tr><td>默认值</td><td>proxy_buffering on;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><blockquote><p>proxy_buffers:该指令用来指定单个连接从代理服务器读取响应的缓存区的个数和大小。</p></blockquote><table><thead><tr><th>语法</th><th>proxy_buffers number size;</th></tr></thead><tbody><tr><td>默认值</td><td>proxy_buffers 8 4k | 8K;(与系统平台有关)</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><p>number:缓冲区的个数</p><p>size:每个缓冲区的大小，缓冲区的总大小就是number*size</p><blockquote><p>proxy_buffer_size:该指令用来设置从被代理服务器获取的第一部分响应数据的大小。保持与proxy_buffers中的size一致即可，当然也可以更小。</p></blockquote><table><thead><tr><th>语法</th><th>proxy_buffer_size size;</th></tr></thead><tbody><tr><td>默认值</td><td>proxy_buffer_size 4k | 8k;(与系统平台有关)</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><blockquote><p>proxy_busy_buffers_size：该指令用来限制同时处于BUSY状态的缓冲总大小。</p></blockquote><table><thead><tr><th>语法</th><th>proxy_busy_buffers_size size;</th></tr></thead><tbody><tr><td>默认值</td><td>proxy_busy_buffers_size 8k|16K;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><blockquote><p>proxy_temp_path:当缓冲区存满后，仍未被Nginx服务器完全接受，响应数据就会被临时存放在磁盘文件上，该指令设置文件路径</p></blockquote><table><thead><tr><th>语法</th><th>proxy_temp_path path;</th></tr></thead><tbody><tr><td>默认值</td><td>proxy_temp_path proxy_temp;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><p>注意path最多设置三层。</p><blockquote><p>proxy_temp_file_write_size：该指令用来设置磁盘上缓冲文件的大小。</p></blockquote><table><thead><tr><th>语法</th><th>proxy_temp_file_write_size size;</th></tr></thead><tbody><tr><td>默认值</td><td>proxy_temp_file_write_size 8K|16K;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><h3 id="通用网站的配置" tabindex="-1">通用网站的配置 <a class="header-anchor" href="#通用网站的配置" aria-label="Permalink to &quot;通用网站的配置&quot;">​</a></h3><p>直接配置在http块上就行</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">http</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    proxy_connect_timeout </span><span style="color:#F78C6C;">10</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    proxy_read_timeout </span><span style="color:#F78C6C;">120</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    proxy_send_timeout </span><span style="color:#F78C6C;">10</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    proxy_buffering on</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    client_body_buffer_size </span><span style="color:#F78C6C;">512k</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    proxy_buffers </span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">64k</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    proxy_buffer_size </span><span style="color:#F78C6C;">16k</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    proxy_busy_buffers_size </span><span style="color:#F78C6C;">128k</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    proxy_temp_file_write_size </span><span style="color:#F78C6C;">128k</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    proxy_temp_path </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">soft</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">nginx</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">temp_buffer</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><blockquote><p>上述的缓冲区参数，是基于每个请求分配的空间，而并不是所有请求的共享空间。当然，具体的参数值还需要根据业务去决定，要综合考虑机器的内存以及每个请求的平均数据大小。最后提一嘴：使用缓冲也可以减少即时传输带来的带宽消耗。</p></blockquote><h1 id="ssl-安全" tabindex="-1">SSL 安全 <a class="header-anchor" href="#ssl-安全" aria-label="Permalink to &quot;SSL 安全&quot;">​</a></h1><h2 id="https-安全概述" tabindex="-1">https 安全概述 <a class="header-anchor" href="#https-安全概述" aria-label="Permalink to &quot;https 安全概述&quot;">​</a></h2><h3 id="什么是安全隔离" tabindex="-1">什么是安全隔离? <a class="header-anchor" href="#什么是安全隔离" aria-label="Permalink to &quot;什么是安全隔离?&quot;">​</a></h3><blockquote><p>通过代理分开了客户端到应用程序服务器端的连接，实现了安全措施。在反向代理之前设置防火墙，仅留一个入口供代理服务器访问。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282207827.png" alt="1589908851340" style="zoom:67%;"><h3 id="如何使用ssl对流量进行加密" tabindex="-1">如何使用SSL对流量进行加密 <a class="header-anchor" href="#如何使用ssl对流量进行加密" aria-label="Permalink to &quot;如何使用SSL对流量进行加密&quot;">​</a></h3><blockquote><p>翻译成大家能熟悉的说法就是将我们常用的http请求转变成https请求，那么这两个之间的区别简单的来说两个都是HTTP协议，只不过https是身披SSL外壳的http.</p></blockquote><blockquote><p>HTTPS是一种通过计算机网络进行安全通信的传输协议。它经由HTTP进行通信，利用SSL/TLS建立全通信，加密数据包，确保数据的安全性。</p></blockquote><p>SSL(Secure Sockets Layer)安全套接层</p><p>TLS(Transport Layer Security)传输层安全</p><p>上述这两个是为网络通信提供安全及数据完整性的一种安全协议，TLS和SSL在传输层和应用层对网络连接进行加密。</p><p>总结来说为什么要使用https:</p><blockquote><p>http协议是明文传输数据，存在安全问题，而https是加密传输，相当于http+ssl，<code>并且可以防止流量劫持</code></p></blockquote><p>Nginx要想使用SSL，需要满足一个条件即需要添加一个模块<code>--with-http_ssl_module</code>,而该模块在编译的过程中又需要OpenSSL的支持，这个我们之前已经准备好了。</p><h2 id="添加ssl-模块⭐" tabindex="-1">添加SSL 模块⭐ <a class="header-anchor" href="#添加ssl-模块⭐" aria-label="Permalink to &quot;添加SSL 模块⭐&quot;">​</a></h2><p>完成 <code>--with-http_ssl_module</code>模块的增量添加</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 进入nginx刚解压的目录</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">/root/nginx-1.21.6</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 通过nginx -V，查看之前安装的模块，带上安装</span></span>
<span class="line"><span style="color:#FFCB6B;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-V</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 安装ssl</span></span>
<span class="line"><span style="color:#FFCB6B;">yum</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-y</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">install</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">openssl</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">openssl-devel</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">make</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">zlib</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">zlib-devel</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">gcc</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">gcc-c++</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">libtool</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">pcre</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">pcre-devel</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 添加模块，要把之前的 --with-http_gzip_static_module 加上</span></span>
<span class="line"><span style="color:#FFCB6B;">./configure</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--with-http_gzip_static_module</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--with-http_ssl_module</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 进行编译</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 进入原来安装好的nginx</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/nginx/sbin/nginx</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 进行备份</span></span>
<span class="line"><span style="color:#FFCB6B;">mv</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/nginx/sbin/nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">nginxold</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 将objs下面的nginx移动到/usr/local/nginx/sbin下</span></span>
<span class="line"><span style="color:#FFCB6B;">cp</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">objs/nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/nginx/sbin</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 在源码目录下执行make upgrade进行升级，这个可以实现不停机添加新模块的功能</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">upgrade</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 查看是否安装成功</span></span>
<span class="line"><span style="color:#FFCB6B;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-V</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301171139880.png" alt="image-20230117113826975" style="zoom:67%;"><h2 id="nginx的ssl指令" tabindex="-1">Nginx的SSL指令 <a class="header-anchor" href="#nginx的ssl指令" aria-label="Permalink to &quot;Nginx的SSL指令&quot;">​</a></h2><p>因为刚才我们介绍过该模块的指令都是通过ngx_http_ssl_module模块来解析的。</p><h3 id="ssl" tabindex="-1">ssl <a class="header-anchor" href="#ssl" aria-label="Permalink to &quot;ssl&quot;">​</a></h3><p>ssl:该指令用来在指定的服务器开启HTTPS,可以使用 listen 443 ssl,后面这种方式更通用些。</p><table><thead><tr><th>语法</th><th>ssl on | off;</th></tr></thead><tbody><tr><td>默认值</td><td>ssl off;</td></tr><tr><td>位置</td><td>http、server</td></tr></tbody></table><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">443</span><span style="color:#A6ACCD;"> ssl</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="ssl-certificate" tabindex="-1">ssl_certificate <a class="header-anchor" href="#ssl-certificate" aria-label="Permalink to &quot;ssl_certificate&quot;">​</a></h3><p>ssl_certificate:为当前这个虚拟主机指定一个带有PEM格式证书的证书。</p><table><thead><tr><th>语法</th><th>ssl_certificate file;</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>http、server</td></tr></tbody></table><h3 id="ssl-certificate-key" tabindex="-1">ssl_certificate_key <a class="header-anchor" href="#ssl-certificate-key" aria-label="Permalink to &quot;ssl_certificate_key&quot;">​</a></h3><p>ssl_certificate_key:该指令用来指定PEM secret key文件的路径</p><table><thead><tr><th>语法</th><th>ssl_ceritificate_key file;</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>http、server</td></tr></tbody></table><h3 id="ssl-session-cache" tabindex="-1">ssl_session_cache <a class="header-anchor" href="#ssl-session-cache" aria-label="Permalink to &quot;ssl_session_cache&quot;">​</a></h3><p>ssl_session_cache:该指令用来配置用于SSL会话的缓存</p><table><thead><tr><th>语法</th><th>ssl_sesion_cache off|none|[builtin[:size]] [shared:name:size]</th></tr></thead><tbody><tr><td>默认值</td><td>ssl_session_cache none;</td></tr><tr><td>位置</td><td>http、server</td></tr></tbody></table><p>off:禁用会话缓存，客户端不得重复使用会话</p><p>none:禁止使用会话缓存，客户端可以重复使用，但是并没有在缓存中存储会话参数</p><p>builtin:内置OpenSSL缓存，仅在一个工作进程中使用。</p><p>shared:所有工作进程之间共享缓存，缓存的相关信息用name和size来指定</p><h3 id="ssl-session-timeout" tabindex="-1">ssl_session_timeout <a class="header-anchor" href="#ssl-session-timeout" aria-label="Permalink to &quot;ssl_session_timeout&quot;">​</a></h3><p>ssl_session_timeout：开启SSL会话功能后，设置客户端能够反复使用储存在缓存中的会话参数时间。</p><table><thead><tr><th>语法</th><th>ssl_session_timeout time;</th></tr></thead><tbody><tr><td>默认值</td><td>ssl_session_timeout 5m;</td></tr><tr><td>位置</td><td>http、server</td></tr></tbody></table><h3 id="ssl-ciphers" tabindex="-1">ssl_ciphers <a class="header-anchor" href="#ssl-ciphers" aria-label="Permalink to &quot;ssl_ciphers&quot;">​</a></h3><p>ssl_ciphers:指出允许的密码，密码指定为OpenSSL支持的格式</p><table><thead><tr><th>语法</th><th>ssl_ciphers ciphers;</th></tr></thead><tbody><tr><td>默认值</td><td>ssl_ciphers HIGH:!aNULL:!MD5;</td></tr><tr><td>位置</td><td>http、server</td></tr></tbody></table><p>可以使用<code>openssl ciphers</code>查看openssl支持的格式。</p><h3 id="ssl-prefer-server-ciphers" tabindex="-1">ssl_prefer_server_ciphers <a class="header-anchor" href="#ssl-prefer-server-ciphers" aria-label="Permalink to &quot;ssl_prefer_server_ciphers&quot;">​</a></h3><p>ssl_prefer_server_ciphers：该指令指定是否服务器密码优先客户端密码</p><table><thead><tr><th>语法</th><th>ssl_perfer_server_ciphers on|off;</th></tr></thead><tbody><tr><td>默认值</td><td>ssl_perfer_server_ciphers off;</td></tr><tr><td>位置</td><td>http、server</td></tr></tbody></table><h2 id="ssl-证书" tabindex="-1">SSL 证书 <a class="header-anchor" href="#ssl-证书" aria-label="Permalink to &quot;SSL 证书&quot;">​</a></h2><blockquote><p>之前我们使用的是自签名的SSL证书，对于浏览器来说是无效的。使用权威机构颁发的SSL证书浏览器才会认为是有效的，这里给大家推荐两种申请免费SSL证书的方法，一种是从阿里云申请，另一种是从FreeSSL申请。</p></blockquote><h3 id="阿里云证书" tabindex="-1">阿里云证书 <a class="header-anchor" href="#阿里云证书" aria-label="Permalink to &quot;阿里云证书&quot;">​</a></h3><p>阿里云上可以申请的免费证书目前只有支持单个域名的DV级SSL证书。比如说你有<code>blog.macrozheng.com</code>和<code>api.macrozheng.com</code>两个二级域名需要使用HTTPS，就需要申请两个SSL证书。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207111611477.png" alt="image-20220711161143356" style="zoom:67%;"><p>申请成功后点击下载Nginx证书即可；</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207111611641.png" alt="image-20220711161158518" style="zoom:50%;"><p>下载完成后解压会有下面两个文件；</p><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">blog.macrozheng.com.key </span><span style="color:#676E95;font-style:italic;"># 证书私钥文件</span></span>
<span class="line"><span style="color:#A6ACCD;">blog.macrozheng.com.pem </span><span style="color:#676E95;font-style:italic;"># 证书文件</span></span></code></pre></div><p>拷贝证书文件到Nginx的指定目录下，然后修改配置文件<code>blog.conf</code>，只要修改证书配置路径即可，修改完成后重启Nginx；</p><div class="language-properties"><button title="Copy Code" class="copy"></button><span class="lang">properties</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#SSL配置</span></span>
<span class="line"><span style="color:#A6ACCD;">ssl_certificate      /usr/share/nginx/html/ssl/blog/blog.macrozheng.com.pem</span><span style="color:#676E95;font-style:italic;">; # 配置证书</span></span>
<span class="line"><span style="color:#A6ACCD;">ssl_certificate_key  /usr/share/nginx/html/ssl/blog/blog.macrozheng.com.key</span><span style="color:#676E95;font-style:italic;">; # 配置证书私钥</span></span></code></pre></div><p>再次通过HTTPS访问<code>blog.macrozheng.com</code>这个域名，发现证书已经有效了，连接也是安全的了。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207111612705.png" alt="image-20220711161225480" style="zoom:50%;"><h3 id="freessl证书" tabindex="-1">FreeSSL证书 <a class="header-anchor" href="#freessl证书" aria-label="Permalink to &quot;FreeSSL证书&quot;">​</a></h3><p>如果你有使用通配符域名的需求，可以上<code>FreeSSL</code>申请SSL证书，不过免费的有效期只有3个月，这就意味着你过3个月就要重新申请一次了。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207111612873.png" alt="image-20220711161239659" style="zoom:50%;"><ul><li>附上官网地址：<a href="https://freessl.cn/" target="_blank" rel="noreferrer">https://freessl.cn/</a></li></ul><h3 id="acme-sh自动申请证书" tabindex="-1">acme.sh自动申请证书 <a class="header-anchor" href="#acme-sh自动申请证书" aria-label="Permalink to &quot;acme.sh自动申请证书&quot;">​</a></h3><p><code>acme.sh</code>脚本实现了<code>acme</code>协议, 可以从<code>letsencrypt</code>生成免费的证书。一般我们申请的证书有效期都是1年，过期就要重新申请了，使用<code>acme.sh</code>脚本可以实现到期自动申请，再也不用担心证书过期了！</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202207111613253.png" alt="image-20220711161317124" style="zoom:67%;"><p>附上官网地址：<a href="https://github.com/acmesh-official/acme.sh" target="_blank" rel="noreferrer">https://github.com/acmesh-official/acme.sh</a></p><h3 id="腾讯云证书-⭐" tabindex="-1">腾讯云证书 ⭐ <a class="header-anchor" href="#腾讯云证书-⭐" aria-label="Permalink to &quot;腾讯云证书 ⭐&quot;">​</a></h3><p><a href="https://console.cloud.tencent.com/ssl/dsc/detail?id=v5cxO2iv" target="_blank" rel="noreferrer">我的证书 - SSL 证书 - 控制台 (tencent.com)</a></p><p>具体配置过程网上挺多的了，也可以使用你购买的某某云，一般都会有<a href="https://link.juejin.cn?target=https%3A%2F%2Fcloud.tencent.com%2Fdocument%2Fproduct%2F400%2F6814" target="_blank" rel="noreferrer">免费申请</a>的服务器证书，安装直接看所在云的操作指南即可。</p><p>我购买的腾讯云提供的亚洲诚信机构颁发的免费证书只能一个域名使用，二级域名什么的需要另外申请，但是申请审批比较快，一般几分钟就能成功，然后下载证书的压缩文件，里面有个 nginx 文件夹，把 <code>xxx.crt</code> 和 <code>xxx.key</code> 文件拷贝到服务器目录，再配置下：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211642406.png" alt="image-20220621164242207" style="zoom:67%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211644801.png" alt="image-20220621164400634" style="zoom:50%;"><p>解压完成后</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211644240.png" alt="image-20220621164453104" style="zoom:67%;"><h3 id="本地生成证书-⭐" tabindex="-1">本地生成证书 ⭐ <a class="header-anchor" href="#本地生成证书-⭐" aria-label="Permalink to &quot;本地生成证书 ⭐&quot;">​</a></h3><p>先要确认当前系统是否有安装openssl，如果没有，则要进行安装</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">openssl version</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 没有openssl，自己进行安装</span></span>
<span class="line"><span style="color:#A6ACCD;">yum </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">y install openssl openssl</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">devel make zlib zlib</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">devel gcc gcc</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">++</span><span style="color:#A6ACCD;"> libtool  pcre pcre</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">devel</span></span></code></pre></div><p>安装下面的命令进行生成</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">root</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">cert</span></span>
<span class="line"><span style="color:#A6ACCD;">cd </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">root</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">cert</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 生成server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">key，执行命令时要输入密码和确认密码，自己随便设置即可</span></span>
<span class="line"><span style="color:#A6ACCD;">openssl genrsa </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">des3 </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">out server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">key </span><span style="color:#F78C6C;">1024</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 生成server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">csr，需要输入上面设置的密码，以及国家姓名等信息</span></span>
<span class="line"><span style="color:#A6ACCD;">openssl req </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">new </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">key server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">key </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">out server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">csr</span></span>
<span class="line"><span style="color:#A6ACCD;">cp server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">key server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">key</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">org</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 需要再次输入上面设置的密码</span></span>
<span class="line"><span style="color:#A6ACCD;">openssl rsa </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">in server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">key</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">org </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">out server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">key</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 成功完成</span></span>
<span class="line"><span style="color:#A6ACCD;">openssl x509 </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">req </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">days </span><span style="color:#F78C6C;">365</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">in server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">csr </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">signkey server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">key </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">out server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">crt</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204300934371.png" alt="image-20220430093426255" style="zoom:80%;"><p>总共生成了4个文件</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204300939115.png" alt="image-20220430093925039" style="zoom:80%;"><h2 id="完整配置-⭐" tabindex="-1">完整配置 ⭐ <a class="header-anchor" href="#完整配置-⭐" aria-label="Permalink to &quot;完整配置 ⭐&quot;">​</a></h2><h3 id="完整示例" tabindex="-1">完整示例 <a class="header-anchor" href="#完整示例" aria-label="Permalink to &quot;完整示例&quot;">​</a></h3><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vim /etc/hosts</span></span>
<span class="line"><span style="color:#A6ACCD;">127.0.0.1 www.itcast.cn</span></span>
<span class="line"><span style="color:#A6ACCD;">ping www.itcast.cn</span></span></code></pre></div><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C3E88D;">server {</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">listen 80;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">server_name www.itcast.cn;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">location / {</span></span>
<span class="line"><span style="color:#89DDFF;">        </span><span style="color:#676E95;font-style:italic;"># 将www.itcast.cn自动加上https://www.itcast.cn</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C3E88D;">rewrite ^(.*) https://www.itcast.cn$1;</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># HTTPS server</span></span>
<span class="line"><span style="color:#C3E88D;">server {</span></span>
<span class="line"><span style="color:#89DDFF;">   </span><span style="color:#676E95;font-style:italic;"># https默认端口就是443，后面的ssl作用等同于ssl on</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">listen       443 ssl;</span></span>
<span class="line"><span style="color:#89DDFF;">   </span><span style="color:#676E95;font-style:italic;"># 到时可以配置成网址：www.renshuo.xyz</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">server_name  www.itcast.cn;</span></span>
<span class="line"><span style="color:#89DDFF;">   </span><span style="color:#676E95;font-style:italic;"># 到时可以配置成对应的pem文件和key文件</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">ssl_certificate      /root/cert/server.crt;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">ssl_certificate_key  /root/cert/server.key;</span></span>
<span class="line"><span style="color:#89DDFF;">   </span><span style="color:#676E95;font-style:italic;">#设置ssl会话缓存</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">ssl_session_cache    shared:SSL:1m;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">ssl_session_timeout  5m;</span></span>
<span class="line"><span style="color:#89DDFF;">   </span><span style="color:#676E95;font-style:italic;"># 密码格式默认就行</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">ssl_ciphers  HIGH:!aNULL:!MD5;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">ssl_prefer_server_ciphers  on;</span></span>
<span class="line"><span style="color:#89DDFF;">   </span><span style="color:#676E95;font-style:italic;"># 地址都会跳转到此，可以配置多个location</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">location /{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C3E88D;">default_type text/html;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#C3E88D;">return 200 &#39;&lt;h1&gt;hello&lt;/h1&gt;&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;">    }</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><p>访问测试：输入www.itcast.cn自动转换成<a href="https://www.itcast.cn/" target="_blank" rel="noreferrer">https://www.itcast.cn/</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211734788.png" alt="image-20220621173419622" style="zoom:80%;"><h1 id="负载均衡" tabindex="-1">负载均衡 <a class="header-anchor" href="#负载均衡" aria-label="Permalink to &quot;负载均衡&quot;">​</a></h1><h2 id="负载均衡概述" tabindex="-1">负载均衡概述 <a class="header-anchor" href="#负载均衡概述" aria-label="Permalink to &quot;负载均衡概述&quot;">​</a></h2><h3 id="单机模式缺点" tabindex="-1">单机模式缺点 <a class="header-anchor" href="#单机模式缺点" aria-label="Permalink to &quot;单机模式缺点&quot;">​</a></h3><blockquote><p>早期的网站流量和业务功能都比较简单，单台服务器足以满足基本的需求，但是随着互联网的发展，业务流量越来越大并且业务逻辑也跟着越来越复杂，单台服务器的性能及单点故障问题就凸显出来了，因此需要多台服务器进行性能的水平扩展及避免单点故障出现。那么如何将不同用户的请求流量分发到不同的服务器上呢？</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223750.png" alt="1591631182469" style="zoom:67%;"><h3 id="系统扩展方案" tabindex="-1">系统扩展方案 <a class="header-anchor" href="#系统扩展方案" aria-label="Permalink to &quot;系统扩展方案&quot;">​</a></h3><blockquote><p>系统的扩展可以分为纵向扩展和横向扩展。</p><p>纵向扩展是从单机的角度出发，通过增加系统的硬件处理能力来提升服务器的处理能力</p><p>横向扩展是通过添加机器来满足大型网站服务的处理能力。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223755.png" alt="1584602513812" style="zoom:67%;"><blockquote><p>这里面涉及到两个重要的角色分别是&quot;应用集群&quot;和&quot;负载均衡器&quot;。</p><p>应用集群：应用部署到多台机器上，组成处理集群，接收负载均衡设备分发的请求，进行处理并返回响应的数据</p><p>负载均衡器:将用户访问的请求根据对应的负载均衡算法，分发到集群中的一台服务器进行处理。</p></blockquote><h3 id="负载均衡的作用" tabindex="-1">负载均衡的作用 <a class="header-anchor" href="#负载均衡的作用" aria-label="Permalink to &quot;负载均衡的作用&quot;">​</a></h3><blockquote><p>1、解决服务器的高并发压力，提高应用程序的处理性能。</p><p>2、提供故障转移，实现高可用。</p><p>3、通过添加或减少服务器数量，增强网站的可扩展性。</p><p>4、在负载均衡器上进行过滤，可以提高系统的安全性。</p></blockquote><h3 id="常用实现负载均衡策略" tabindex="-1">常用实现负载均衡策略 <a class="header-anchor" href="#常用实现负载均衡策略" aria-label="Permalink to &quot;常用实现负载均衡策略&quot;">​</a></h3><h4 id="用户手动选择" tabindex="-1">用户手动选择 <a class="header-anchor" href="#用户手动选择" aria-label="Permalink to &quot;用户手动选择&quot;">​</a></h4><blockquote><p>这种方式比较原始，只要实现的方式就是在网站主页上面提供不同线路、不同服务器链接方式，让用户来选择自己访问的具体服务器，来实现负载均衡。</p></blockquote><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223759.png" alt="1584602887881"></p><h4 id="dns轮询方式" tabindex="-1">DNS轮询方式 <a class="header-anchor" href="#dns轮询方式" aria-label="Permalink to &quot;DNS轮询方式&quot;">​</a></h4><blockquote><p>DNS：域名系统（服务）协议（DNS）是一种分布式网络目录服务，主要用于域名与 IP 地址的相互转换。一个域名可以绑定多个ip地址，这样就有了多台服务器</p></blockquote><blockquote><p>大多域名注册商都支持对同一个主机名添加多条A记录，这就是DNS轮询，DNS服务器将解析请求按照A记录的顺序，随机分配到不同的IP上，这样就能完成简单的负载均衡。DNS轮询的成本非常低，在一些不重要的服务器，被经常使用。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223756.png" alt="1591010973996" style="zoom:40%;"><p>如下是我们为某一个域名添加的IP地址，用2台服务器来做负载均衡。</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223757.png" alt="1590064506355"></p><p>验证:</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">ping renshuo</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">xyz</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204301050630.png" alt="image-20220430105000541" style="zoom:80%;"><p>清空本地的dns缓存</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">ipconfig</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">flushdns</span></span></code></pre></div><p>我们发现使用DNS来实现轮询，不需要投入过多的成本，虽然DNS轮询成本低廉，但是DNS负载均衡存在明显的缺点。</p><blockquote><p>1.可靠性低</p><p>假设一个域名DNS轮询多台服务器，如果其中的一台服务器发生故障，那么所有的访问该服务器的请求将不会有所回应，即使你将该服务器的IP从DNS中去掉，但是由于各大宽带接入商将众多的DNS存放在缓存中，以节省访问时间，导致DNS不会实时更新。所以DNS轮流上一定程度上解决了负载均衡问题，但是却存在可靠性不高的缺点。</p><p>2.负载均衡不均衡</p><p>DNS负载均衡采用的是简单的轮询负载算法，不能区分服务器的差异，不能反映服务器的当前运行状态，不能做到为性能好的服务器多分配请求，另外本地计算机也会缓存已经解析的域名到IP地址的映射，这也会导致使用该DNS服务器的用户在一定时间内访问的是同一台Web服务器，从而引发Web服务器减的负载不均衡。</p><p>负载不均衡则会导致某几台服务器负荷很低，而另外几台服务器负荷确很高，处理请求的速度慢，配置高的服务器分配到的请求少，而配置低的服务器分配到的请求多。</p></blockquote><h3 id="四-七层负载均衡-重点" tabindex="-1">四/七层负载均衡(重点) <a class="header-anchor" href="#四-七层负载均衡-重点" aria-label="Permalink to &quot;四/七层负载均衡(重点)&quot;">​</a></h3><blockquote><p>介绍四/七层负载均衡之前，我们先了解一个概念，OSI(open system interconnection),叫开放式系统互联模型，这个是由国际标准化组织ISO指定的一个不基于具体机型、操作系统或公司的网络体系结构。该模型将网络通信的工作分为七层。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223768.png" alt="1584693830966" style="zoom:50%;"><blockquote><p>应用层：为应用程序提供网络服务。</p><p>表示层：对数据进行格式化、编码、加密、压缩等操作。</p><p>会话层：建立、维护、管理会话连接。</p><p>传输层：建立、维护、管理端到端的连接，常见的有TCP/UDP。</p><p>网络层：IP寻址和路由选择</p><p>数据链路层：控制网络层与物理层之间的通信。</p><p>物理层：比特流传输。</p></blockquote><p>所谓四层负载均衡指的是OSI七层模型中的传输层，主要是基于IP+PORT的负载均衡</p><blockquote><p>实现四层负载均衡的方式： 硬件：F5 BIG-IP、Radware等 软件：LVS、Nginx、Hayproxy等</p></blockquote><p>所谓的七层负载均衡指的是在应用层，主要是基于虚拟的URL或主机IP的负载均衡</p><blockquote><p>实现七层负载均衡的方式： 软件：Nginx、Hayproxy等</p></blockquote><p><strong>四层和七层负载均衡的区别</strong></p><blockquote><p>四层负载均衡数据包是在底层就进行了分发，而七层负载均衡数据包则在最顶端进行分发，所以四层负载均衡的效率比七层负载均衡的要高。 四层负载均衡不识别域名，而七层负载均衡识别域名。</p></blockquote><p>处理四层和七层负载以为其实还有二层、三层负载均衡，二层是在数据链路层基于mac地址来实现负载均衡，三层是在网络层一般采用虚拟IP地址的方式实现负载均衡。</p><p><strong>实际环境采用的模式</strong></p><blockquote><p>四层负载(LVS)+七层负载(Nginx)</p></blockquote><h2 id="七层负载均衡⭐" tabindex="-1">七层负载均衡⭐ <a class="header-anchor" href="#七层负载均衡⭐" aria-label="Permalink to &quot;七层负载均衡⭐&quot;">​</a></h2><p>Nginx要实现七层负载均衡<code>需要用到proxy_pass代理模块配置</code>。Nginx默认安装支持这个模块，我们不需要再做任何处理。Nginx的负载均衡是在Nginx的反向代理基础上把用户的请求根据指定的算法分发到一组【upstream虚拟服务池】</p><h3 id="基本指令" tabindex="-1">基本指令 <a class="header-anchor" href="#基本指令" aria-label="Permalink to &quot;基本指令&quot;">​</a></h3><h4 id="upstream指令" tabindex="-1">upstream指令 <a class="header-anchor" href="#upstream指令" aria-label="Permalink to &quot;upstream指令&quot;">​</a></h4><blockquote><p>该指令是用来定义一组服务器，它们可以是监听不同端口的服务器，并且也可以是同时监听TCP和Unix socket的服务器。服务器可以指定不同的权重，默认为1。</p></blockquote><table><thead css-module="."><tr><th>语法</th><th>upstream name</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>http</td></tr></tbody></table><h4 id="server指令" tabindex="-1">server指令 <a class="header-anchor" href="#server指令" aria-label="Permalink to &quot;server指令&quot;">​</a></h4><blockquote><p>该指令用来指定后端服务器的名称和一些参数，可以使用域名、IP、端口或者unix socket</p></blockquote><table><thead><tr><th>语法</th><th>server name [paramerters]</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>upstream</td></tr></tbody></table><h3 id="实现流程⭐" tabindex="-1">实现流程⭐ <a class="header-anchor" href="#实现流程⭐" aria-label="Permalink to &quot;实现流程⭐&quot;">​</a></h3><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223124.png" alt="1590248160635" style="zoom:67%;"><h4 id="定义web服务器" tabindex="-1">定义web服务器 <a class="header-anchor" href="#定义web服务器" aria-label="Permalink to &quot;定义web服务器&quot;">​</a></h4><p>自定义server，写在nginx.conf中即可</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen   </span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">        return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">&lt;h1&gt;192.168.22.146:9001&lt;/h1&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen   </span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">        return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">&lt;h1&gt;192.168.22.146:9002&lt;/h1&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen   </span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    default_type text</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">        return </span><span style="color:#F78C6C;">200</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">&lt;h1&gt;192.168.22.146:9003&lt;/h1&gt;</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h4 id="定义负载均衡器" tabindex="-1">定义负载均衡器 <a class="header-anchor" href="#定义负载均衡器" aria-label="Permalink to &quot;定义负载均衡器&quot;">​</a></h4><blockquote><p>这个可以设置在一个单独的服务器中，目前为了测试，就把他们放在一个服务器中</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h4 id="验证测试" tabindex="-1">验证测试 <a class="header-anchor" href="#验证测试" aria-label="Permalink to &quot;验证测试&quot;">​</a></h4><p>访问网址<a href="http://192.168.22.146:8083/%EF%BC%8C%E5%8F%91%E7%8E%B0%E4%B8%8A%E9%9D%A2%E4%B8%89%E4%B8%AA%E7%BD%91%E5%9D%80%E8%BD%AE%E6%B5%81%E8%A2%AB%E8%AE%BF%E9%97%AE" target="_blank" rel="noreferrer">http://192.168.22.146:8083/，发现上面三个网址轮流被访问</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206210940809.png" alt="image-20220621094058711" style="zoom:67%;"><p>或者另一种方式验证：启动CMD</p><div class="language-c"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">curl http:</span><span style="color:#676E95;font-style:italic;">//192.168.22.130:8083/</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206210943913.png" alt="image-20220621094301830" style="zoom:67%;"><h3 id="参数配置-⭐" tabindex="-1">参数配置 ⭐ <a class="header-anchor" href="#参数配置-⭐" aria-label="Permalink to &quot;参数配置 ⭐&quot;">​</a></h3><p>代理服务器在负责均衡调度中的状态有以下几个：</p><table><thead><tr><th>状态</th><th>概述</th></tr></thead><tbody><tr><td>down</td><td>当前的server暂时不参与负载均衡</td></tr><tr><td>backup</td><td>预留的备份服务器</td></tr><tr><td>max_fails</td><td>允许请求失败的次数</td></tr><tr><td>fail_timeout</td><td>经过max_fails失败后, 服务暂停时间</td></tr><tr><td>max_conns</td><td>限制最大的接收连接数</td></tr></tbody></table><h4 id="down-永久不可用" tabindex="-1">down 永久不可用 <a class="header-anchor" href="#down-永久不可用" aria-label="Permalink to &quot;down 永久不可用&quot;">​</a></h4><blockquote><p>down:将该服务器标记为永久不可用，那么该代理服务器将不参与负载均衡。</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.22.129</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#A6ACCD;"> down</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.22.129</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.22.129</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>该状态一般会对需要停机维护的服务器进行设置。用chrome浏览器进行访问时可能会出现只访问9002的情况，这时可以用其他浏览器或者cmd的curl进行测试：curl <a href="http://192.168.22.129:8083/" target="_blank" rel="noreferrer">http://192.168.22.129:8083/</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204301107193.png" alt="image-20220430110735110" style="zoom:80%;"><h4 id="backup-备份服务器" tabindex="-1">backup 备份服务器 <a class="header-anchor" href="#backup-备份服务器" aria-label="Permalink to &quot;backup 备份服务器&quot;">​</a></h4><blockquote><p>backup:将该服务器标记为备份服务器，只有当主服务器不可用时，才将用来传递请求。此时需要将9094端口的访问禁止掉来模拟下唯一能对外提供访问的服务宕机以后，backup的备份服务器就要开始对外提供服务，此时为了测试验证，我们需要使用防火墙来进行拦截。</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#A6ACCD;"> down</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#A6ACCD;"> backup</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h4 id="max-conns-最大连接数" tabindex="-1">max_conns 最大连接数 <a class="header-anchor" href="#max-conns-最大连接数" aria-label="Permalink to &quot;max_conns 最大连接数&quot;">​</a></h4><blockquote><p>max_conns=number:用来设置代理服务器同时活动链接的最大数量，<strong>默认为0，表示不限制</strong>，使用该配置可以根据后端服务器处理请求的并发量来进行设置，防止后端服务器被压垮。</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#A6ACCD;"> max_conns</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1000</span><span style="color:#A6ACCD;"> backup</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#A6ACCD;"> max_conns</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1500</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#A6ACCD;"> max_conns</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">2000</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h4 id="max-fails和fail-timeout" tabindex="-1">max_fails和fail_timeout <a class="header-anchor" href="#max-fails和fail-timeout" aria-label="Permalink to &quot;max_fails和fail_timeout&quot;">​</a></h4><p>max_fails=number:设置<code>允许请求代理服务器失败的次数</code>，默认为1。</p><p>fail_timeout=time:设置经过max_fails失败后，<code>服务暂停的时间</code>，默认是10秒。</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.133</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#A6ACCD;"> max_conns</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">10</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.133</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#A6ACCD;"> backup</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.133</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#A6ACCD;"> max_fails</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;"> fail_timeout</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">15</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="可选项配置" tabindex="-1">可选项配置 <a class="header-anchor" href="#可选项配置" aria-label="Permalink to &quot;可选项配置&quot;">​</a></h3><div class="language-yml"><button title="Copy Code" class="copy"></button><span class="lang">yml</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C3E88D;">upstream back_end{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">server 127.0.0.1:8081 weight=3 max_conns=1000 fail_timeout=10s max_fails=2;</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;"># 限制每个 worker 子进程与上游服务器空闲长连接的最大数量</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">keepalive 32;</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;"># 单个长连接可以处理的最多 HTTP 请求个数</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">keepalive_requests 50;</span></span>
<span class="line"><span style="color:#89DDFF;">    </span><span style="color:#676E95;font-style:italic;"># 空闲长连接的最长保持时间</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#C3E88D;">keepalive_timeout 30s;</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><h3 id="负载均衡策略⭐" tabindex="-1">负载均衡策略⭐ <a class="header-anchor" href="#负载均衡策略⭐" aria-label="Permalink to &quot;负载均衡策略⭐&quot;">​</a></h3><p>介绍完Nginx负载均衡的相关指令后，我们已经能实现将用户的请求分发到不同的服务器上，那么除了采用默认的分配方式以外，我们还能采用什么样的负载算法? Nginx的upstream支持如下六种方式的分配算法，分别是:</p><table><thead><tr><th>算法名称</th><th>说明</th></tr></thead><tbody><tr><td>轮询</td><td>默认方式</td></tr><tr><td>weight</td><td>权重方式</td></tr><tr><td>ip_hash</td><td>依据ip分配方式</td></tr><tr><td>least_conn</td><td>依据最少连接方式</td></tr><tr><td>url_hash</td><td>依据URL分配方式</td></tr><tr><td>fair</td><td>依据响应时间方式</td></tr></tbody></table><h4 id="轮询-默认" tabindex="-1">轮询 默认 <a class="header-anchor" href="#轮询-默认" aria-label="Permalink to &quot;轮询 默认&quot;">​</a></h4><blockquote><p>是upstream模块<strong>负载均衡默认的策略</strong>。每个请求会按时间顺序逐个分配到不同的后端服务器。轮询不需要额外的配置，因为默认的方式就是轮询</p></blockquote><blockquote><p>每个请求按时间顺序逐一分配到不同的后端服务器，也就是说第一次请求分配到第一台服务器上，第二次请求分配到第二台服务器上，如果只有两台服务器，第三次请求继续分配到第一台上，这样循环轮询下去，也就是服务器接收请求的比例是 1:1， 如果后端服务器down掉，能自动剔除。轮询是默认配置，不需要太多的配置</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h4 id="weight-加权轮询" tabindex="-1">weight 加权轮询 <a class="header-anchor" href="#weight-加权轮询" aria-label="Permalink to &quot;weight 加权轮询&quot;">​</a></h4><blockquote><p>指定轮询几率，weight和访问比率成正比, 也就是服务器接收请求的比例就是各自配置的weight的比例，用于后端服务器性能不均的情况,比如服务器性能差点就少接收点请求，服务器性能好点就多处理点请求。</p></blockquote><blockquote><p>weight=number:用来设置服务器的权重，默认为1，权重数据越大，被分配到请求的几率越大；该权重值，主要是针对实际工作环境中不同的后端服务器硬件配置进行调整的，所有此策略比较适合服务器的硬件配置差别比较大的情况。权重越大不表示一定会选择到它，而是请求次数越多越会按照权重的比例分布</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#A6ACCD;"> weight</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">10</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#A6ACCD;"> weight</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">5</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> backup是指热备，只有当</span><span style="color:#F78C6C;">9001</span><span style="color:#A6ACCD;">和</span><span style="color:#F78C6C;">9002</span><span style="color:#A6ACCD;">都宕机的情况下才走</span><span style="color:#F78C6C;">9003</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#A6ACCD;"> weight</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;"> backup</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>进行访问：<a href="http://192.168.22.130:8083/" target="_blank" rel="noreferrer">http://192.168.22.130:8083/</a></p><h4 id="ip-hash" tabindex="-1">ip_hash <a class="header-anchor" href="#ip-hash" aria-label="Permalink to &quot;ip_hash&quot;">​</a></h4><blockquote><p>主要解决session共享问题，但无法保证负载均衡，会导致少数服务器被很多用户访问，上面的2种方式都有一个问题，那就是下一个请求来的时候请求可能分发到另外一个服务器，当我们的程序不是无状态的时候(采用了session保存数据)，这时候就有一个很大的很问题了</p></blockquote><blockquote><p>比如把登录信息保存到了session中，那么跳转到另外一台服务器的时候就需要重新登录了，所以很多时候我们需要一个客户只访问一个服务器，那么就需要用iphash了，iphash的每个请求按访问ip的hash结果分配，这样每个访客固定访问一个后端服务器，可以解决session的问题。</p></blockquote><table><thead><tr><th>语法</th><th>ip_hash;</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>upstream</td></tr></tbody></table><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    ip_hash</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><blockquote><p>需要额外多说一点的是使用ip_hash指令无法保证后端服务器的负载均衡，可能导致有些后端服务器接收到的请求多，有些后端服务器接收的请求少，而且设置后端服务器权重等方法将不起作用。推荐使用redis来解决登录session会话保存的问题</p></blockquote><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223131.png" alt="1591706748677"></p><h4 id="least-conn-最少连接" tabindex="-1">least_conn 最少连接 <a class="header-anchor" href="#least-conn-最少连接" aria-label="Permalink to &quot;least_conn 最少连接&quot;">​</a></h4><blockquote><p>此负载均衡策略适合请求处理时间长短不一造成服务器过载的情况。</p></blockquote><blockquote><p>最少连接，把请求转发给连接数较少的后端服务器。轮询算法是把请求平均的转发给各个后端，使它们的负载大致相同；但是，有些请求占用的时间很长，会导致其所在的后端负载较高。这种情况下，least_conn这种方式就可以达到更好的负载均衡效果。</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    least_conn</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.22.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223136.png" alt="1591809623736"></p><h4 id="url-hash" tabindex="-1">url_hash <a class="header-anchor" href="#url-hash" aria-label="Permalink to &quot;url_hash&quot;">​</a></h4><blockquote><p>按访问url的hash结果来分配请求，使每个url定向到同一个后端服务器，要配合缓存命中来使用。**同一个资源多次请求，可能会到达不同的服务器上，导致不必要的多次下载，缓存命中率不高，以及一些资源时间的浪费。**而使用url_hash，可以使得同一个url（也就是同一个资源请求）会到达同一台服务器，一旦缓存住了资源，再此收到请求，就可以从缓存中读取。</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	hash </span><span style="color:#89DDFF;">&amp;</span><span style="color:#A6ACCD;">request_uri</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 如果</span><span style="color:#89DDFF;">&amp;</span><span style="color:#A6ACCD;">request_uri换成对应IP</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>访问如下地址：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">http</span><span style="color:#89DDFF;">://</span><span style="color:#F78C6C;">192.168.200.133</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">a</span></span>
<span class="line"><span style="color:#A6ACCD;">http</span><span style="color:#89DDFF;">://</span><span style="color:#F78C6C;">192.168.200.133</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">b</span></span>
<span class="line"><span style="color:#A6ACCD;">http</span><span style="color:#89DDFF;">://</span><span style="color:#F78C6C;">192.168.200.133</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">c</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223144.png" alt="1591812222306"></p><h4 id="fair-智能切换" tabindex="-1">fair 智能切换 <a class="header-anchor" href="#fair-智能切换" aria-label="Permalink to &quot;fair 智能切换&quot;">​</a></h4><blockquote><p>fair采用的不是内建负载均衡使用的轮换的均衡算法，而是可以根据页面大小、加载时间长短智能的进行负载均衡。那么如何使用第三方模块的fair负载均衡策略。但是如何直接使用会报错，因为fair属于第三方模块实现的负载均衡。需要添加<code>nginx-upstream-fair</code></p></blockquote><h5 id="fair模块下载配置" tabindex="-1">fair模块下载配置 <a class="header-anchor" href="#fair模块下载配置" aria-label="Permalink to &quot;fair模块下载配置&quot;">​</a></h5><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 下载地址为:https://github.com/gnosek/nginx-upstream-fair</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 将下载的文件上传到服务器并进行解压缩 /root目录</span></span>
<span class="line"><span style="color:#FFCB6B;">unzip</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">nginx-upstream-fair-master.zip</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 重命名资源</span></span>
<span class="line"><span style="color:#FFCB6B;">mv</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">nginx-upstream-fair-master</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">fair</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 使用./configure命令将资源添加到Nginx模块中，要通过nginx -V把nginx之前添加的模块加到configue后面</span></span>
<span class="line"><span style="color:#FFCB6B;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-V</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/nginx-1.23.2</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">clean</span></span>
<span class="line"><span style="color:#FFCB6B;">./configure</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--with-http_gzip_static_module</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--with-http_ssl_module</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--add-module=/root/fair</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 编译</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span></span></code></pre></div><blockquote><p>编译可能会出现如下错误，ngx_http_upstream_srv_conf_t结构中缺少default_port</p></blockquote><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223142.png" alt="1584941470457"></p><blockquote><p>解决方案:在Nginx的源码中 src/http/ngx_http_upstream.h,找到<code>ngx_http_upstream_srv_conf_s</code>，在模块中添加添加default_port属性：/root/nginx-1.23.2/src/http</p></blockquote><div class="language-c"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">in_port_t</span><span style="color:#A6ACCD;">	   default_port；</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301171610497.png" alt="image-20230117161016316" style="zoom:67%;"><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 再次进行编译执行后续步骤即可</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 将objs目录下的nginx二进制执行文件移动到nginx安装目录下的sbin目录中</span></span>
<span class="line"><span style="color:#FFCB6B;">mv</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">objs/nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/nginx/sbin</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 执行更新命令</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">upgrade</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 查看是否安装成功</span></span>
<span class="line"><span style="color:#FFCB6B;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-V</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301171618827.png" alt="image-20230117161809624" style="zoom:67%;"><h5 id="fair-模块使用" tabindex="-1">fair 模块使用 <a class="header-anchor" href="#fair-模块使用" aria-label="Permalink to &quot;fair 模块使用&quot;">​</a></h5><div class="language-coffeescript"><button title="Copy Code" class="copy"></button><span class="lang">coffeescript</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend{</span></span>
<span class="line"><span style="color:#A6ACCD;">	fair;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server 192.168.200.146:9001;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server 192.168.200.146:9002;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server 192.168.200.146:9003;</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server {</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen 8083;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location /{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http://backend;</span></span>
<span class="line"><span style="color:#A6ACCD;">	}</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><blockquote><p>上面介绍了Nginx常用的负载均衡的策略，有人说是5种，是把轮询和加权轮询归为一种，也有人说是6种。那么在咱们以后的开发中到底使用哪种，这个需要根据实际项目的应用场景来决定的。</p></blockquote><h3 id="负载均衡案例" tabindex="-1">负载均衡案例 <a class="header-anchor" href="#负载均衡案例" aria-label="Permalink to &quot;负载均衡案例&quot;">​</a></h3><h4 id="对所有请求实现轮询规则的负载均衡" tabindex="-1">对所有请求实现轮询规则的负载均衡 <a class="header-anchor" href="#对所有请求实现轮询规则的负载均衡" aria-label="Permalink to &quot;对所有请求实现轮询规则的负载均衡&quot;">​</a></h4><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h4 id="对所有请求实现加权轮询的负载均衡" tabindex="-1">对所有请求实现加权轮询的负载均衡 <a class="header-anchor" href="#对所有请求实现加权轮询的负载均衡" aria-label="Permalink to &quot;对所有请求实现加权轮询的负载均衡&quot;">​</a></h4><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#A6ACCD;"> weight</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">7</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#A6ACCD;"> weight</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">5</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#A6ACCD;"> weight</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h4 id="对特定资源实现负载均衡" tabindex="-1">对特定资源实现负载均衡 <a class="header-anchor" href="#对特定资源实现负载均衡" aria-label="Permalink to &quot;对特定资源实现负载均衡&quot;">​</a></h4><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream videobackend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">upstream filebackend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9004</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen </span><span style="color:#F78C6C;">8084</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 以video和file开头的url分别对应不同的服务器</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">video</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">videobackend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">file</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">filebackend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h4 id="对不同域名实现负载均衡" tabindex="-1">对不同域名实现负载均衡 <a class="header-anchor" href="#对不同域名实现负载均衡" aria-label="Permalink to &quot;对不同域名实现负载均衡&quot;">​</a></h4><p>先修改hosts</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vim </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">etc</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">hosts</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">127.0.0.1</span><span style="color:#A6ACCD;"> www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span></span>
<span class="line"><span style="color:#F78C6C;">127.0.0.1</span><span style="color:#A6ACCD;"> www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span></span></code></pre></div><p>nginx.conf</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream itcastbackend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">upstream itheimabackend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9004</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen	</span><span style="color:#F78C6C;">8085</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itcast</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">itcastbackend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen	</span><span style="color:#F78C6C;">8086</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">cn</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">itheimabackend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>注意只能在虚拟机内部浏览器访问，访问：www.itcast.cn:8085</p><h4 id="实现带有url重写的负载均衡" tabindex="-1">实现带有URL重写的负载均衡 <a class="header-anchor" href="#实现带有url重写的负载均衡" aria-label="Permalink to &quot;实现带有URL重写的负载均衡&quot;">​</a></h4><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9001</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9002</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server </span><span style="color:#F78C6C;">192.168.200.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">9003</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	listen	</span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 对所有</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">file的url重写成</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server开头的，加上last表示会重新匹配</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server开头的location</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">file</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		rewrite ^(</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">file</span><span style="color:#89DDFF;">/.*</span><span style="color:#A6ACCD;">) </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">$</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> last</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">	location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">		proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h2 id="四层负载均衡" tabindex="-1">四层负载均衡 <a class="header-anchor" href="#四层负载均衡" aria-label="Permalink to &quot;四层负载均衡&quot;">​</a></h2><blockquote><p>Nginx在1.9之后，增加了一个stream模块，用来实现四层协议的转发、代理、负载均衡等。stream模块的用法跟http的用法类似，允许我们配置一组TCP或者UDP等协议的监听，然后通过proxy_pass来转发我们的请求，通过upstream添加多个后端服务，实现负载均衡。</p></blockquote><blockquote><p>四层协议负载均衡的实现，一般都会用到LVS、HAProxy、F5等，要么很贵要么配置很麻烦，而Nginx的配置相对来说更简单，更能快速完成工作。</p></blockquote><h3 id="stream-模块添加" tabindex="-1">stream 模块添加 <a class="header-anchor" href="#stream-模块添加" aria-label="Permalink to &quot;stream 模块添加&quot;">​</a></h3><p>Nginx默认是没有编译这个模块的，需要使用到stream模块，那么需要在编译的时候加上<code>--with-stream</code>。</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 查询当前Nginx的配置参数</span></span>
<span class="line"><span style="color:#FFCB6B;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-V</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 将nginx安装目录下sbin目录中的nginx二进制文件进行更名</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/nginx/sbin</span></span>
<span class="line"><span style="color:#FFCB6B;">mv</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">nginxold</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 进入Nginx的安装目录</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/nginx-1.23.2</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 执行make clean清空之前编译的内容</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">clean</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 使用configure来配置参数</span></span>
<span class="line"><span style="color:#FFCB6B;">./configure</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--with-http_gzip_static_module</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--with-stream</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 使用make命令进行编译</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 将objs目录下的nginx二进制执行文件移动到nginx安装目录下的sbin目录中</span></span>
<span class="line"><span style="color:#FFCB6B;">mv</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">objs/nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/nginx/sbin</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 执行更新命令</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">upgrade</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 查看是否安装成功</span></span>
<span class="line"><span style="color:#FFCB6B;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-V</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301171635898.png" alt="image-20230117163515656" style="zoom:67%;"><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 需要重启nginx，不然不会生效</span></span>
<span class="line"><span style="color:#FFCB6B;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-s</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">stop</span></span>
<span class="line"><span style="color:#FFCB6B;">nginx</span></span></code></pre></div><h3 id="四层负载均衡指令" tabindex="-1">四层负载均衡指令 <a class="header-anchor" href="#四层负载均衡指令" aria-label="Permalink to &quot;四层负载均衡指令&quot;">​</a></h3><h4 id="stream指令" tabindex="-1">stream指令 <a class="header-anchor" href="#stream指令" aria-label="Permalink to &quot;stream指令&quot;">​</a></h4><p>该指令提供在其中指定流服务器指令的配置文件上下文。和http指令同级。</p><table><thead css-module="."><tr><th>语法</th><th>stream</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>main</td></tr></tbody></table><h4 id="upstream指令-1" tabindex="-1">upstream指令 <a class="header-anchor" href="#upstream指令-1" aria-label="Permalink to &quot;upstream指令&quot;">​</a></h4><p>该指令和http的upstream指令是类似的。</p><h3 id="redis-负载均衡⭐" tabindex="-1">Redis 负载均衡⭐ <a class="header-anchor" href="#redis-负载均衡⭐" aria-label="Permalink to &quot;Redis 负载均衡⭐&quot;">​</a></h3><h4 id="需求分析" tabindex="-1">需求分析 <a class="header-anchor" href="#需求分析" aria-label="Permalink to &quot;需求分析&quot;">​</a></h4><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204282223442.png" alt="1591897178807" style="zoom:50%;"><h4 id="实现步骤" tabindex="-1">实现步骤 <a class="header-anchor" href="#实现步骤" aria-label="Permalink to &quot;实现步骤&quot;">​</a></h4><p>准备Redis服务器,在一条服务器上准备三个Redis，端口分别是6379,6378，分别启动，即可获取两个Redis.并查看</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">cp</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/etc/redis.conf</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/reids/redis1.conf</span></span>
<span class="line"><span style="color:#FFCB6B;">redis-server</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/reids/redis1.conf</span></span>
<span class="line"><span style="color:#FFCB6B;">cp</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/reids/redis1.conf</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/reids/redis2.conf</span></span>
<span class="line"><span style="color:#FFCB6B;">redis-server</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/reids/redis2.conf</span></span>
<span class="line"><span style="color:#FFCB6B;">ps</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-ef</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">grep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">redis</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301171646303.png" alt="image-20230117164637109" style="zoom:67%;"><p>使用Nginx将请求分发到不同的Redis服务器上。</p><p>nginx.conf配置</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 在http块外部，和http块同级</span></span>
<span class="line"><span style="color:#A6ACCD;">stream </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> Redis 服务器</span></span>
<span class="line"><span style="color:#A6ACCD;">   upstream redisbackend </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 将请求转发到不同的redis服务器</span></span>
<span class="line"><span style="color:#A6ACCD;">        server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">6379</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">6380</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        listen  </span><span style="color:#F78C6C;">81</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass redisbackend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>访问测试</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">redis-cli -h 192.168.22.146 -p 81</span></span></code></pre></div><h3 id="tomcat-负载均衡" tabindex="-1">Tomcat 负载均衡 <a class="header-anchor" href="#tomcat-负载均衡" aria-label="Permalink to &quot;Tomcat 负载均衡&quot;">​</a></h3><p>准备Tomcat服务器(和上面Redis不影响)</p><p>1.上传tomcat的安装包，<code>apache-tomcat-8.5.56.tar.gz</code></p><p>2.将安装包进行解压缩</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">tar</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-zxvf</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">apache-tomcat-8.5.59.tar.gz</span></span></code></pre></div><p>3.进入tomcat的bin目录</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">apache-tomcat-8.5.59/bin</span></span>
<span class="line"><span style="color:#FFCB6B;">./startup.sh</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301171720651.png" alt="image-20230117172014437" style="zoom:67%;"><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">stream </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    upstream backend </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        server </span><span style="color:#F78C6C;">192.168.22.146</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8080</span><span style="color:#A6ACCD;"> max_fails</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;"> fail_timeout</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">30s</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        listen </span><span style="color:#F78C6C;">82</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_connect_timeout </span><span style="color:#F78C6C;">1s</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_timeout </span><span style="color:#F78C6C;">3s</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>访问测试：<a href="http://192.168.22.146:82/" target="_blank" rel="noreferrer">http://192.168.22.146:82/</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301171753804.png" alt="image-20230117175315568" style="zoom:67%;"><h1 id="动静分离" tabindex="-1">动静分离 <a class="header-anchor" href="#动静分离" aria-label="Permalink to &quot;动静分离&quot;">​</a></h1><h2 id="动静分离概述" tabindex="-1">动静分离概述 <a class="header-anchor" href="#动静分离概述" aria-label="Permalink to &quot;动静分离概述&quot;">​</a></h2><h3 id="什么是动静分离" tabindex="-1">什么是动静分离? <a class="header-anchor" href="#什么是动静分离" aria-label="Permalink to &quot;什么是动静分离?&quot;">​</a></h3><blockquote><p>动:后台应用程序的业务处理；静:网站的静态资源(html,javaScript,css,images等文件)</p></blockquote><blockquote><p>分离:将两者进行分开部署访问，提供用户进行访问。举例说明就是以后所有和静态资源相关的内容都交给Nginx来部署访问，非静态内容则交个类似于Tomcat的服务器来部署访问。</p></blockquote><h3 id="为什么要动静分离" tabindex="-1">为什么要动静分离? <a class="header-anchor" href="#为什么要动静分离" aria-label="Permalink to &quot;为什么要动静分离?&quot;">​</a></h3><blockquote><p>动静分离应该是听的次数较多的性能优化方案，那先思考一个问题：<strong>「为什么需要做动静分离呢？它带来的好处是什么？」</strong> 其实这个问题也并不难回答，当你搞懂了网站的本质后，自然就理解了动静分离的重要性。淘宝网为例</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301180935421.png" alt="image-20230118093506170" style="zoom:80%;"><blockquote><p>当浏览器输入<code>www.taobao.com</code>访问淘宝首页时，打开开发者调试工具可以很明显的看到，首页加载会出现<code>100+</code>的请求数，而正常项目开发时，静态资源一般会放入到<code>resources/static/</code>目录下：</p></blockquote><blockquote><p>在项目上线部署时，这些静态资源会一起打成包，那此时思考一个问题：<strong>「假设淘宝也是这样干的，那么首页加载时的请求最终会去到哪儿被处理？」</strong> 答案毋庸置疑，首页100+的所有请求都会来到部署WEB服务的机器处理，那则代表着一个客户端请求淘宝首页，就会对后端服务器造成100+的并发请求。这对于后端服务器的压力是尤为巨大的。</p></blockquote><blockquote><p>但此时不妨分析看看，首页100+的请求中，是不是至少有60+是属于*.js、<em>.css、</em>.html、*.jpg.....这类静态资源的请求呢？答案是Yes。</p></blockquote><blockquote><p>既然有这么多请求属于静态的，这些资源大概率情况下，长时间也不会出现变动，那为何还要让这些请求到后端再处理呢？能不能在此之前就提前处理掉？当然<code>OK</code>，因此经过分析之后能够明确一点：<strong>「做了动静分离之后，至少能够让后端服务减少一半以上的并发量。」</strong> 到此时大家应该明白了动静分离能够带来的性能收益究竟有多大。</p></blockquote><h3 id="如何实现动静分离" tabindex="-1">如何实现动静分离 <a class="header-anchor" href="#如何实现动静分离" aria-label="Permalink to &quot;如何实现动静分离&quot;">​</a></h3><blockquote><p>实现动静分离的方式很多，比如静态资源可以部署到CDN、Nginx等服务器上，动态资源可以部署到Tomcat,weblogic或者websphere上。本次课程只要使用Nginx+Tomcat来实现动静分离。</p></blockquote><h2 id="实现步骤-1" tabindex="-1">实现步骤 <a class="header-anchor" href="#实现步骤-1" aria-label="Permalink to &quot;实现步骤&quot;">​</a></h2><p>①先在部署<code>Nginx</code>的机器，<code>Nginx</code>目录下创建一个目录<code>static_resources</code>：</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">mkdir</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">static_resources</span></span></code></pre></div><p>②将项目中所有的静态资源全部拷贝到该目录下，而后将项目中的静态资源移除重新打包。</p><p>③稍微修改一下<code>nginx.conf</code>的配置，增加一条<code>location</code>匹配规则：</p><div class="language-scss"><button title="Copy Code" class="copy"></button><span class="lang">scss</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location ~ .</span><span style="color:#FFCB6B;">*</span><span style="color:#A6ACCD;">\.(</span><span style="color:#FFCB6B;">html</span><span style="color:#A6ACCD;">|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    root   /soft/nginx/static_resources</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    expires 7d</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>然后照常启动<code>nginx</code>和移除了静态资源的<code>WEB</code>服务，你会发现原本的样式、<code>js</code>效果、图片等依旧有效</p><p>其中<code>static</code>目录下的<code>nginx_style.css</code>文件已被移除，但效果依旧存在（绿色字体+蓝色大边框)：</p><p><strong>最后提一嘴，也可以将静态资源上传到文件服务器中，然后<code>location</code>中配置一个新的<code>upstream</code>指向。</strong></p><h2 id="动静分离实战" tabindex="-1">动静分离实战 <a class="header-anchor" href="#动静分离实战" aria-label="Permalink to &quot;动静分离实战&quot;">​</a></h2><p>准备：启动SpringBoot项目，在nginx/html/web上传图片</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211808741.png" alt="image-20220621180836592" style="zoom:67%;"><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream webservice</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   server </span><span style="color:#F78C6C;">192.168.1.107</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8080</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   listen       </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   server_name  localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">动态资源，这样的缺点是只能写hello一个路径，写</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">就是表示可以访问所有路径</span></span>
<span class="line"><span style="color:#A6ACCD;">   location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">         proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">webservice</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 拦截静态资源请求</span></span>
<span class="line"><span style="color:#A6ACCD;">   location </span><span style="color:#89DDFF;">~/.*\.</span><span style="color:#A6ACCD;">(png</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">jpg</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">gif</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">js</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">css)</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">         root html</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">web</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>当正常访问时：<a href="http://192.168.22.130/getAccount" target="_blank" rel="noreferrer">http://192.168.22.130/getAccount</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211809644.png" alt="image-20220621180910476" style="zoom:50%;"><p>当访问图片时：<a href="http://192.168.22.130/g2.jpg" target="_blank" rel="noreferrer">http://192.168.22.130/g2.jpg</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211810268.png" alt="image-20220621181023882" style="zoom:50%;"><h2 id="负载均衡-动静分离" tabindex="-1">负载均衡+动静分离 <a class="header-anchor" href="#负载均衡-动静分离" aria-label="Permalink to &quot;负载均衡+动静分离&quot;">​</a></h2><p>在使用Nginx和Tomcat部署项目的时候，我们使用的是一台Nginx服务器和一台Tomcat服务器，效果图如下:</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204301712100.png" alt="1604494256017" style="zoom:67%;"><p>那么问题来了，如果Tomcat的真的宕机了，整个系统就会不完整，所以如何解决上述问题，一台服务器容易宕机，那就多搭建几台Tomcat服务器，这样的话就提升了后的服务器的可用性。这也就是我们常说的集群，搭建Tomcat的集群需要用到了Nginx的反向代理和赋值均衡的知识，具体如何来实现?我们先来分析下原理</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204301712106.png" alt="1604494269848" style="zoom:67%;"><p>环境准备：</p><p>(1)准备3台tomcat,使用端口进行区分[实际环境应该是三台服务器]，修改server.ml，将端口修改分别修改为8080,88081,8082</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 启动</span><span style="color:#F78C6C;">8080</span><span style="color:#A6ACCD;">服务</span></span>
<span class="line"><span style="color:#A6ACCD;">java </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">jar demo</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">jar</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 启动</span><span style="color:#F78C6C;">8081</span><span style="color:#A6ACCD;">、</span><span style="color:#F78C6C;">8082</span><span style="color:#A6ACCD;">服务</span></span>
<span class="line"><span style="color:#A6ACCD;">java </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">jar demo</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">jar  </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">8081</span></span>
<span class="line"><span style="color:#A6ACCD;">java </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">jar demo</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">jar  </span><span style="color:#89DDFF;">--</span><span style="color:#A6ACCD;">server</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">port</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">8082</span></span></code></pre></div><p>(2)启动tomcat并访问测试，</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">http</span><span style="color:#89DDFF;">://</span><span style="color:#F78C6C;">192.168.0.155</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8080</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">hello</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">http</span><span style="color:#89DDFF;">://</span><span style="color:#F78C6C;">192.168.0.155</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8081</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">hello</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">http</span><span style="color:#89DDFF;">://</span><span style="color:#F78C6C;">192.168.0.155</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8082</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">hello</span></span></code></pre></div><p>(3)在Nginx对应的配置文件中添加如下内容:</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 和上面对比，主要修改了upstream，让服务器负载均衡</span></span>
<span class="line"><span style="color:#A6ACCD;">upstream webservice</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        server </span><span style="color:#F78C6C;">192.168.0.155</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8080</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        server </span><span style="color:#F78C6C;">192.168.0.155</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8081</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        server </span><span style="color:#F78C6C;">192.168.0.155</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8082</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">       listen       </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">       server_name  localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">       root html</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">web</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">动态资源</span></span>
<span class="line"><span style="color:#A6ACCD;">        location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">               </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 处理跨域</span></span>
<span class="line"><span style="color:#A6ACCD;">               add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Origin </span><span style="color:#89DDFF;">*;</span></span>
<span class="line"><span style="color:#A6ACCD;">               add_header Access</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Allow</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Methods GET</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">POST</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">PUT</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">DELETE</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">               proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">webservice</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        listen    </span><span style="color:#F78C6C;">8089</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        server_name  localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">        location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">            root html</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">web</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">            index  index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">htm</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">         </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">静态资源</span></span>
<span class="line"><span style="color:#A6ACCD;">        location </span><span style="color:#89DDFF;">~/.*\.</span><span style="color:#A6ACCD;">(png</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">jpg</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">gif</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">js)</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">            root html</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">web</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">            gzip on</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>最后进行访问：<a href="http://192.168.22.129:8089/index.html" target="_blank" rel="noreferrer">http://192.168.22.129:8089/index.html</a></p><p>好了，完成了上述环境的部署，我们已经解决了Tomcat的高可用性，一台服务器宕机，还有其他两条对外提供服务，同时也可以实现后台服务器的不间断更新。但是新问题出现了，上述环境中，如果是Nginx宕机了呢，那么整套系统都将服务对外提供服务了，这个如何解决？</p><h1 id="限流" tabindex="-1">限流 <a class="header-anchor" href="#限流" aria-label="Permalink to &quot;限流&quot;">​</a></h1><p>Nginx按请求速率限速模块使用的是<strong>漏桶算法</strong>，即能够强行保证请求的实时处理速度不会超过设置的阈值。</p><p>Nginx的限流主要通过修改<code>nginx.conf</code>文件来进行，有两种限流方式：</p><ul><li>通过<code>请求数</code>进行限流</li><li>通过<code>连接数</code>进行限流</li></ul><h2 id="漏桶算法" tabindex="-1">漏桶算法 <a class="header-anchor" href="#漏桶算法" aria-label="Permalink to &quot;漏桶算法&quot;">​</a></h2><blockquote><ul><li>水（请求）从上方倒入水桶，从水桶下方流出（被处理）；</li><li>来不及流出的水存在水桶中（缓冲），以固定速率流出；</li><li>水桶满后水溢出（丢弃）。</li><li>这个算法的核心是：缓存请求、匀速处理、多余的请求直接丢弃。</li><li>相比漏桶算法，令牌桶算法不同之处在于它不但有一只“桶”，还有个队列，这个桶是用来存放令牌的，队列才是用来存放请求的。</li></ul></blockquote><h2 id="参数分析" tabindex="-1">参数分析 <a class="header-anchor" href="#参数分析" aria-label="Permalink to &quot;参数分析&quot;">​</a></h2><p>参数解释：</p><blockquote><ul><li>$binary_remote_addr：binary_目的是缩写内存占用，remote_addr表示通过IP地址来限流</li><li>zone:iplimit是一块内存区域（记录访问频率信息）,20m是指这块内存区域的大小</li><li>rate: 1r/s = 1 request / second，类似于100/m（每分钟100次请求）</li><li>burst: burst=2,设置一个大小为2的缓存区域，当大量请求到来，请求数量超过限流频率时，将其放入缓冲区域</li><li>nodelay: 缓冲区满了后直接返回503异常</li><li>另外我们也可以在location下通过配置 limit_req_status 504或limit_conn_status 504来修改默认errorCode</li></ul></blockquote><h2 id="根据ip限流" tabindex="-1">根据IP限流 <a class="header-anchor" href="#根据ip限流" aria-label="Permalink to &quot;根据IP限流&quot;">​</a></h2><p>单个ip访问次数限制</p><blockquote><ul><li>第一个参数：$binary_remote_addr 表示通过remote_addr这个标识来做限制，“binary_”的目的是缩写内存占用量，是限制同一客户端ip地址。</li><li>第二个参数：zone=one:10m表示生成一个大小为10M，名字为one的内存区域，用来存储访问的频次信息。</li><li>第三个参数：rate=10r/s表示允许同一个客户端的访问频次是每秒10次，还可以有比如30r/m的。</li></ul></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">limit_req_zone $binary_remote_addr zone</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">iplimit:</span><span style="color:#F78C6C;">10m</span><span style="color:#A6ACCD;"> rate</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1r</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">s</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 设置返回状态码，可选</span></span>
<span class="line"><span style="color:#A6ACCD;">limit_req_status </span><span style="color:#F78C6C;">503</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.1.107</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8089</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 根据ip地址限制流量</span></span>
<span class="line"><span style="color:#A6ACCD;">        limit_req zone</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">iplimit burst</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> nodelay</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>访问测试：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.23/202206281506176.png" alt="image-20220628150610095" style="zoom:67%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.23/202206281506374.png" alt="image-20220628150627288" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.23/202206281505722.png" alt="image-20220628150550611" style="zoom:50%;"><p>可以发现再刷新频率较低时，能正常访问服务，当频率超过1r/s时，会抛503异常</p><h2 id="根据服务器限流" tabindex="-1">根据服务器限流 <a class="header-anchor" href="#根据服务器限流" aria-label="Permalink to &quot;根据服务器限流&quot;">​</a></h2><p>上面是根据单ip进行修改，我们也同时支持根据服务器级别进行限流，将$binary_remote_addr修改为$server_name即可</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">limit_req_zone  $server_name zone</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">iplimit:</span><span style="color:#F78C6C;">10m</span><span style="color:#A6ACCD;"> rate</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1r</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">s</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">limit_req_status </span><span style="color:#F78C6C;">503</span><span style="color:#89DDFF;">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.1.107</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8089</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen </span><span style="color:#F78C6C;">8083</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 根据ip地址限制流量</span></span>
<span class="line"><span style="color:#A6ACCD;">        limit_req zone</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">iplimit burst</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> nodelay</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h2 id="" tabindex="-1"><a class="header-anchor" href="#" aria-label="Permalink to &quot;&quot;">​</a></h2><h1 id="高可用-keepalived" tabindex="-1">高可用 Keepalived <a class="header-anchor" href="#高可用-keepalived" aria-label="Permalink to &quot;高可用 Keepalived&quot;">​</a></h1><h2 id="keepalived-环境搭建" tabindex="-1">Keepalived 环境搭建 <a class="header-anchor" href="#keepalived-环境搭建" aria-label="Permalink to &quot;Keepalived 环境搭建&quot;">​</a></h2><p><a href="http://192.168.0.199:8082/hello" target="_blank" rel="noreferrer">http://192.168.0.199:8082/hello</a></p><table><thead><tr><th>VIP 虚拟IP</th><th>IP</th><th>主/从</th></tr></thead><tbody><tr><td></td><td>192.168.22.128</td><td>Master</td></tr><tr><td>192.168.22.151</td><td></td><td></td></tr><tr><td></td><td>192.168.200.150</td><td>Backup</td></tr></tbody></table><h3 id="环境搭建" tabindex="-1">环境搭建 <a class="header-anchor" href="#环境搭建" aria-label="Permalink to &quot;环境搭建&quot;">​</a></h3><blockquote><p>步骤1:从官方网站下载keepalived,官网地址：<a href="https://keepalived.org/" target="_blank" rel="noreferrer">https://keepalived.org/</a></p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301181213088.png" alt="image-20230118121336006" style="zoom:67%;"><blockquote><p>步骤2:将下载的资源上传到服务器</p></blockquote><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 3、创建keepalived目录，方便管理资源</span></span>
<span class="line"><span style="color:#FFCB6B;">mkdir</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">keepalived</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 4、将压缩文件进行解压缩，解压缩到指定的目录</span></span>
<span class="line"><span style="color:#FFCB6B;">tar</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-xvf</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">keepalived-2.2.7.tar.gz</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">-C</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">keepalived/</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">keepalived/keepalived-2.2.7</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 5、对keepalived进行配置</span></span>
<span class="line"><span style="color:#FFCB6B;">./configure</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--sysconf=/etc</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--prefix=/usr/local</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 6、编译和安装</span></span>
<span class="line"><span style="color:#FFCB6B;">make</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">make</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">install</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">keepalived</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-v</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301181211645.png" alt="image-20230118121103319" style="zoom:67%;"><blockquote><p>忘记安装配置的目录，则通过如下命令找到：</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">whereis keepalived</span></span></code></pre></div><h3 id="文件位置" tabindex="-1">文件位置 <a class="header-anchor" href="#文件位置" aria-label="Permalink to &quot;文件位置&quot;">​</a></h3><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 配置文件位置,主要操作的就是该文件 </span></span>
<span class="line"><span style="color:#FFCB6B;">/etc/keepalived/keepalived.conf</span></span>
<span class="line"><span style="color:#FFCB6B;">cp</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">keepalived.conf.sample</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">keepalived.conf</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 可执行脚本位置,启动和关闭keepalived</span></span>
<span class="line"><span style="color:#FFCB6B;">/usr/local/sbin/keepalived</span></span></code></pre></div><h3 id="前提条件" tabindex="-1">前提条件 <a class="header-anchor" href="#前提条件" aria-label="Permalink to &quot;前提条件&quot;">​</a></h3><blockquote><p>nginx必须启动，不然启动不生效</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">upstream backend</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    server </span><span style="color:#F78C6C;">192.168.0.199</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8082</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    listen </span><span style="color:#F78C6C;">80</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    server_name localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    location </span><span style="color:#89DDFF;">/{</span></span>
<span class="line"><span style="color:#A6ACCD;">        proxy_pass http:</span><span style="color:#89DDFF;">//</span><span style="color:#A6ACCD;">backend</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h2 id="实战案例" tabindex="-1">实战案例 <a class="header-anchor" href="#实战案例" aria-label="Permalink to &quot;实战案例&quot;">​</a></h2><h3 id="主服务器" tabindex="-1">主服务器 <a class="header-anchor" href="#主服务器" aria-label="Permalink to &quot;主服务器&quot;">​</a></h3><blockquote><p>注意：其他不需要的配置要注释起来，推荐先备份配置文件，在修改新的配置文件</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">!</span><span style="color:#A6ACCD;"> Configuration File for keepalived</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">global_defs </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 自带的邮件提醒服务，建议用独立的监控或第三方SMTP，也可选择配置邮件发送</span></span>
<span class="line"><span style="color:#A6ACCD;">   notification_email </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">     acassen</span><span style="color:#89DDFF;">@</span><span style="color:#A6ACCD;">firewall</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">loc</span></span>
<span class="line"><span style="color:#A6ACCD;">     failover</span><span style="color:#89DDFF;">@</span><span style="color:#A6ACCD;">firewall</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">loc</span></span>
<span class="line"><span style="color:#A6ACCD;">     sysadmin</span><span style="color:#89DDFF;">@</span><span style="color:#A6ACCD;">firewall</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">loc</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">   notification_email_from Alexandre</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">Cassen</span><span style="color:#89DDFF;">@</span><span style="color:#A6ACCD;">firewall</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">loc</span></span>
<span class="line"><span style="color:#A6ACCD;">   smtp_server </span><span style="color:#F78C6C;">192.168.200.1</span></span>
<span class="line"><span style="color:#A6ACCD;">   smtp_connect_timeout </span><span style="color:#F78C6C;">30</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 高可用集群主机身份标识(集群中主机身份标识名称不能重复，建议配置成本机IP)⭐</span></span>
<span class="line"><span style="color:#A6ACCD;">   router_id </span><span style="color:#F78C6C;">192.168.22.150</span></span>
<span class="line"><span style="color:#A6ACCD;">   vrrp_skip_check_adv_addr</span></span>
<span class="line"><span style="color:#A6ACCD;">   vrrp_strict</span></span>
<span class="line"><span style="color:#A6ACCD;">   vrrp_garp_interval </span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#A6ACCD;">   vrrp_gna_interval </span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 定时运行的脚本文件配置</span></span>
<span class="line"><span style="color:#A6ACCD;">vrrp_script check_nginx_pid_restart </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 之前编写的nginx重启脚本的所在位置</span></span>
<span class="line"><span style="color:#A6ACCD;"> script </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/etc/keepalived/check_nginx_pid_restart.sh</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 每间隔</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">秒执行一次</span></span>
<span class="line"><span style="color:#A6ACCD;"> interval </span><span style="color:#F78C6C;">3</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 如果脚本中的条件成立，重启一次则权重</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">20</span></span>
<span class="line"><span style="color:#A6ACCD;"> weight </span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">20</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 定义虚拟路由，VI_1为虚拟路由的标示符（可自定义名称）</span></span>
<span class="line"><span style="color:#A6ACCD;">vrrp_instance VI_1 </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 当前节点的身份标识：用来决定主从（MASTER为主机，BACKUP为从机）</span></span>
<span class="line"><span style="color:#A6ACCD;">    state MASTER</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 绑定虚拟IP的网络接口，根据自己的机器的网卡配置，必须改成ens33</span></span>
<span class="line"><span style="color:#A6ACCD;">    interface ens33</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 虚拟路由的ID号，主从两个节点设置必须一样</span></span>
<span class="line"><span style="color:#A6ACCD;">    virtual_router_id </span><span style="color:#F78C6C;">51</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 填写本机IP</span></span>
<span class="line"><span style="color:#A6ACCD;">    mcast_src_ip </span><span style="color:#F78C6C;">192.168.22.150</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 节点权重优先级，主节点要比从节点优先级高</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 优先级高的设置nopreempt，解决异常恢复后再次抢占造成的脑裂问题</span></span>
<span class="line"><span style="color:#A6ACCD;">    nopreempt</span></span>
<span class="line"><span style="color:#A6ACCD;">    priority </span><span style="color:#F78C6C;">100</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 组播信息发送间隔，两个节点设置必须一样，默认</span><span style="color:#F78C6C;">1s</span><span style="color:#A6ACCD;">（类似于心跳检测）</span></span>
<span class="line"><span style="color:#A6ACCD;">    advert_int </span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#A6ACCD;">    authentication </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        auth_type PASS</span></span>
<span class="line"><span style="color:#A6ACCD;">        auth_pass </span><span style="color:#F78C6C;">1111</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 将track_script块加入instance配置块</span></span>
<span class="line"><span style="color:#A6ACCD;">    track_script </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 执行Nginx监控的脚本</span></span>
<span class="line"><span style="color:#A6ACCD;">        check_nginx_pid_restart</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    virtual_ipaddress </span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 虚拟IP(VIP)，也可扩展，可配置多个</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F78C6C;">192.168.22.151</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span></code></pre></div><h3 id="备份服务器" tabindex="-1">备份服务器 <a class="header-anchor" href="#备份服务器" aria-label="Permalink to &quot;备份服务器&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">!</span><span style="color:#A6ACCD;"> Configuration File for keepalived</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">global_defs </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   notification_email </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">     acassen</span><span style="color:#89DDFF;">@</span><span style="color:#A6ACCD;">firewall</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">loc</span></span>
<span class="line"><span style="color:#A6ACCD;">     failover</span><span style="color:#89DDFF;">@</span><span style="color:#A6ACCD;">firewall</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">loc</span></span>
<span class="line"><span style="color:#A6ACCD;">     sysadmin</span><span style="color:#89DDFF;">@</span><span style="color:#A6ACCD;">firewall</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">loc</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">   notification_email_from Alexandre</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">Cassen</span><span style="color:#89DDFF;">@</span><span style="color:#A6ACCD;">firewall</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">loc</span></span>
<span class="line"><span style="color:#A6ACCD;">   smtp_server </span><span style="color:#F78C6C;">192.168.200.1</span></span>
<span class="line"><span style="color:#A6ACCD;">   smtp_connect_timeout </span><span style="color:#F78C6C;">30</span></span>
<span class="line"><span style="color:#A6ACCD;">   router_id </span><span style="color:#F78C6C;">192.168.22.128</span></span>
<span class="line"><span style="color:#A6ACCD;">   vrrp_skip_check_adv_addr</span></span>
<span class="line"><span style="color:#A6ACCD;">   vrrp_strict</span></span>
<span class="line"><span style="color:#A6ACCD;">   vrrp_garp_interval </span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#A6ACCD;">   vrrp_gna_interval </span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">vrrp_instance VI_1 </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    state BACKUP</span></span>
<span class="line"><span style="color:#A6ACCD;">    interface ens33</span></span>
<span class="line"><span style="color:#A6ACCD;">    virtual_router_id </span><span style="color:#F78C6C;">51</span></span>
<span class="line"><span style="color:#A6ACCD;">    mcast_src_ip </span><span style="color:#F78C6C;">192.168.22.128</span></span>
<span class="line"><span style="color:#A6ACCD;">    priority </span><span style="color:#F78C6C;">90</span></span>
<span class="line"><span style="color:#A6ACCD;">    advert_int </span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#A6ACCD;">    authentication </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        auth_type PASS</span></span>
<span class="line"><span style="color:#A6ACCD;">        auth_pass </span><span style="color:#F78C6C;">1111</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    virtual_ipaddress </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#F78C6C;">192.168.22.151</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><h3 id="脚本配置" tabindex="-1">脚本配置 <a class="header-anchor" href="#脚本配置" aria-label="Permalink to &quot;脚本配置&quot;">​</a></h3><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vi</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/etc/keepalived/check_nginx_pid_restart.sh</span></span>
<span class="line"><span style="color:#FFCB6B;">chmod</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">+x</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">/etc/keepalived/check_nginx_pid_restart.sh</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/sh</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 通过ps指令查询后台的nginx进程数，并将其保存在变量nginx_number中</span></span>
<span class="line"><span style="color:#A6ACCD;">nginx_number</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">ps</span><span style="color:#C3E88D;"> -C nginx --no-header </span><span style="color:#89DDFF;">|</span><span style="color:#C3E88D;"> </span><span style="color:#FFCB6B;">wc</span><span style="color:#C3E88D;"> -l</span><span style="color:#89DDFF;">`</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 判断后台是否还有Nginx进程在运行</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $nginx_number </span><span style="color:#89DDFF;">-eq</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">];</span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 如果后台查询不到`Nginx`进程存在，则执行重启指令</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">nginx</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-c</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/nginx/conf/nginx.conf</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 重启后等待1s后，再次查询后台进程数</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">sleep</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 如果重启后依旧无法查询到nginx进程</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">ps</span><span style="color:#C3E88D;"> -C nginx --no-header </span><span style="color:#89DDFF;">|</span><span style="color:#C3E88D;"> </span><span style="color:#FFCB6B;">wc</span><span style="color:#C3E88D;"> -l</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-eq</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">];</span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#676E95;font-style:italic;"># 将keepalived主机下线，将虚拟IP漂移给从机，从机上线接管Nginx服务</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">systemctl</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">stop</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">keepalived.service</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><h3 id="启动keepalived" tabindex="-1">启动keepalived <a class="header-anchor" href="#启动keepalived" aria-label="Permalink to &quot;启动keepalived&quot;">​</a></h3><p>分别启动两台服务器的keepalived</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/usr/local/sbin</span></span>
<span class="line"><span style="color:#FFCB6B;">./keepalived</span></span>
<span class="line"><span style="color:#FFCB6B;">ps</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-ef</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">grep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">keepalived</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.1.30/202301181658973.png" alt="image-20230118165856881" style="zoom:67%;"><h3 id="访问测试-1" tabindex="-1">访问测试 <a class="header-anchor" href="#访问测试-1" aria-label="Permalink to &quot;访问测试&quot;">​</a></h3><blockquote><p>网站：<a href="http://192.168.22.151/hello%EF%BC%8C%E6%88%90%E5%8A%9F%E8%AE%BF%E9%97%AE%EF%BC%8C%E5%90%AF%E5%8A%A8keepalived%E4%B9%8B%E5%89%8D%EF%BC%8C%E5%92%B1%E4%BB%AC%E5%85%88%E4%BD%BF%E7%94%A8%E5%91%BD%E4%BB%A4" target="_blank" rel="noreferrer">http://192.168.22.151/hello，成功访问，启动keepalived之前，咱们先使用命令</a> <code>ip a</code>,查看192.168.200.133和192.168.200.122这两台服务器的IP情况</p></blockquote><blockquote><p>通过上述的测试，我们会发现，虚拟IP(VIP)会在MASTER节点上，当MASTER节点上的keepalived出问题以后，因为BACKUP无法收到MASTER发出的VRRP状态通过信息，就会直接升为MASTER。VIP也会&quot;漂移&quot;到新的MASTER。</p></blockquote><blockquote><p>从上图中可以明显看见虚拟<code>IP</code>已经成功挂载，但另外一台机器<code>192.168.22.128</code>并不会挂载这个虚拟<code>IP</code>，只有当主机下线后，作为从机的<code>192.168.22.128</code>才会上线，接替<code>VIP</code>。最后测试一下外网是否可以正常与<code>VIP</code>通信，即在<code>Windows</code>中直接<code>ping VIP</code>：</p></blockquote><h1 id="下载站点" tabindex="-1">下载站点 <a class="header-anchor" href="#下载站点" aria-label="Permalink to &quot;下载站点&quot;">​</a></h1><h2 id="什么是下载站点" tabindex="-1">什么是下载站点 <a class="header-anchor" href="#什么是下载站点" aria-label="Permalink to &quot;什么是下载站点&quot;">​</a></h2><p>首先我们先要清楚什么是下载站点?</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206201843998.png" alt="image-20220620184337841" style="zoom:50%;"><p>我们先来看一个网站<code>http://nginx.org/download/</code>这个我们刚开始学习Nginx的时候给大家看过这样的网站，该网站主要就是用来提供用户来下载相关资源的网站，就叫做下载网站。</p><h2 id="语法详解" tabindex="-1">语法详解 <a class="header-anchor" href="#语法详解" aria-label="Permalink to &quot;语法详解&quot;">​</a></h2><p>如何制作一个下载站点:</p><p>nginx使用的是模块ngx_http_autoindex_module来实现的，该模块处理以斜杠(&quot;/&quot;)结尾的请求，并生成目录列表。</p><p>nginx编译的时候会自动加载该模块，但是该模块默认是关闭的，我们需要使用下来指令来完成对应的配置</p><p>autoindex:启用或禁用目录列表输出</p><table><thead><tr><th>语法</th><th>autoindex on|off;</th></tr></thead><tbody><tr><td>默认值</td><td>autoindex off;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><p>autoindex_exact_size:对应HTLM格式，指定是否在目录列表展示文件的详细大小</p><p>默认为on，显示出文件的确切大小，单位是bytes。 改为off后，显示出文件的大概大小，单位是kB或者MB或者GB</p><table><thead><tr><th>语法</th><th>autoindex_exact_size on|off;</th></tr></thead><tbody><tr><td>默认值</td><td>autoindex_exact_size on;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><p>autoindex_format：设置目录列表的格式</p><table><thead><tr><th>语法</th><th>autoindex_format html|xml|json|jsonp;</th></tr></thead><tbody><tr><td>默认值</td><td>autoindex_format html;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><p>注意:该指令在1.7.9及以后版本中出现</p><p>autoindex_localtime:对应HTML格式，是否在目录列表上显示时间</p><p>默认为off，显示的文件时间为GMT时间 改为on后，显示的文件时间为文件的服务器时间</p><table><thead><tr><th>语法</th><th>autoindex_localtime on | off;</th></tr></thead><tbody><tr><td>默认值</td><td>autoindex_localtime off;</td></tr><tr><td>位置</td><td>http、server、location</td></tr></tbody></table><h2 id="案例演示-⭐" tabindex="-1">案例演示 ⭐ <a class="header-anchor" href="#案例演示-⭐" aria-label="Permalink to &quot;案例演示 ⭐&quot;">​</a></h2><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mkdir </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">download</span></span></code></pre></div><p>在该文件夹上传若干文件</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206201849328.png" alt="image-20220620184938268" style="zoom:67%;"><p>nginx.conf配置方式如下:</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 注意路径：最后会拼接root</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">访问路径，即</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">download文件所在位置</span></span>
<span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">download</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    root </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    autoindex on</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 打开 autoindex，，可选参数有 on </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> off</span></span>
<span class="line"><span style="color:#A6ACCD;">    autoindex_exact_size on</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 修改为off，以KB、MB、GB显示文件大小，默认为on，以bytes显示确切⼤⼩</span></span>
<span class="line"><span style="color:#A6ACCD;">    autoindex_format html</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 以html的方式进行格式化，可选参数有 html </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> json </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> xml</span></span>
<span class="line"><span style="color:#A6ACCD;">    autoindex_localtime on</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 显示的⽂件时间为⽂件的服务器时间。默认为off，显示的⽂件时间为GMT时间</span></span>
<span class="line"><span style="color:#A6ACCD;">    charset </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">utf-8</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 中文乱码解决</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>进行访问：<a href="http://192.168.22.130/download/" target="_blank" rel="noreferrer">http://192.168.22.130/download/</a></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206201900605.png" alt="image-20220620190042540" style="zoom:50%;"><p>点击即可进行下载，文件夹也可以点进去</p><h2 id="安全下载" tabindex="-1">安全下载 <a class="header-anchor" href="#安全下载" aria-label="Permalink to &quot;安全下载&quot;">​</a></h2><blockquote><p>对应系统资源的访问，我们往往需要限制谁能访问，谁不能访问。这块就是我们通常所说的认证部分，认证需要做的就是根据用户输入的用户名和密码来判定用户是否为合法用户，如果是则放行访问，如果不是则拒绝访问。</p></blockquote><p>Nginx对应用户认证这块是通过ngx_http_auth_basic_module模块来实现的，它允许通过使用&quot;HTTP基本身份验证&quot;协议验证用户名和密码来限制对资源的访问。默认情况下nginx是已经安装了该模块，如果不需要则使用--without-http_auth_basic_module。</p><p>该模块的指令比较简单，</p><p>（1）auth_basic:使用“ HTTP基本认证”协议启用用户名和密码的验证</p><table><thead><tr><th>语法</th><th>auth_basic string|off;</th></tr></thead><tbody><tr><td>默认值</td><td>auth_basic off;</td></tr><tr><td>位置</td><td>http,server,location,limit_except</td></tr></tbody></table><p>开启后，服务端会返回401，指定的字符串会返回到客户端，给用户以提示信息，但是不同的浏览器对内容的展示不一致。</p><p>（2）auth_basic_user_file:指定用户名和密码所在文件</p><table><thead><tr><th>语法</th><th>auth_basic_user_file file;</th></tr></thead><tbody><tr><td>默认值</td><td>—</td></tr><tr><td>位置</td><td>http,server,location,limit_except</td></tr></tbody></table><p>指定文件路径，该文件中的用户名和密码的设置，密码需要进行加密。可以采用工具自动生成</p><p>实现步骤:</p><p>1.nginx.conf添加如下内容</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">download</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    root </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    autoindex on</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    autoindex_exact_size on</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    autoindex_format html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    autoindex_localtime on</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 中文乱码解决</span></span>
<span class="line"><span style="color:#A6ACCD;">    charset </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">utf-8</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    auth_basic </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">please input your auth</span><span style="color:#89DDFF;">&#39;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 注意路径要和下面的路径相同，不然要修改路径</span></span>
<span class="line"><span style="color:#A6ACCD;">    auth_basic_user_file htpasswd</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>2.我们需要使用<code>htpasswd</code>工具生成</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">yum install </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">y httpd</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">tools</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 创建一个新文件记录用户名和密码，需要输入两次确认密码</span></span>
<span class="line"><span style="color:#A6ACCD;">htpasswd </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">c </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">nginx</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">htpasswd username </span></span>
<span class="line"><span style="color:#A6ACCD;">htpasswd </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">b </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">nginx</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">htpasswd username password </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 在指定文件新增一个用户名和密码</span></span>
<span class="line"><span style="color:#A6ACCD;">htpasswd </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">D </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">nginx</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">htpasswd username </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 从指定文件删除一个用户信息</span></span>
<span class="line"><span style="color:#A6ACCD;">htpasswd </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">v </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">usr</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">local</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">nginx</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">conf</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">htpasswd username </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 验证用户名和密码是否正确</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206201908865.png" alt="image-20220620190833740" style="zoom:67%;"><p>进行访问：</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206201909117.png" alt="image-20220620190925040" style="zoom:67%;"><p>输入上面设置的账号密码即可进入</p><blockquote><p>上述方式虽然能实现用户名和密码的验证，但是大家也看到了，所有的用户名和密码信息都记录在文件里面，如果用户量过大的话，这种方式就显得有点麻烦了，这时候我们就得通过后台业务代码来进行用户权限的校验了。</p></blockquote><h1 id="服务器购买和部署⭐" tabindex="-1">服务器购买和部署⭐ <a class="header-anchor" href="#服务器购买和部署⭐" aria-label="Permalink to &quot;服务器购买和部署⭐&quot;">​</a></h1><h2 id="域名配置-购买" tabindex="-1">域名配置 &amp; 购买 <a class="header-anchor" href="#域名配置-购买" aria-label="Permalink to &quot;域名配置 &amp; 购买&quot;">​</a></h2><h3 id="本机配置" tabindex="-1">本机配置 <a class="header-anchor" href="#本机配置" aria-label="Permalink to &quot;本机配置&quot;">​</a></h3><blockquote><p>hosts是一个没有扩展名的系统文件，可以用记事本等工具打开，其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”，当用户在浏览器中输入一个需要登录的网址时，系统会首先自动从hosts文件中寻找对应的IP地址，一旦找到，系统会立即打开对应网页，如果没有找到，则系统会再将网址提交DNS域名解析服务器进行IP地址的解析</p></blockquote><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">windows:C:\Windows\System32\drivers\etc</span></span>
<span class="line"><span style="color:#FFCB6B;">centos：/etc/hosts</span></span></code></pre></div><blockquote><p>因为域名是要收取一定的费用，所以我们可以使用修改hosts文件来制作一些虚拟域名来使用。需要修改 <code>/etc/hosts</code>文件来添加</p></blockquote><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/etc/hosts</span></span>
<span class="line"><span style="color:#FFCB6B;">127.0.0.1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">www.itcast.cn</span></span>
<span class="line"><span style="color:#FFCB6B;">127.0.0.1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">www.itheima.cn</span></span></code></pre></div><p>注意：只能在本机上进行访问</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022/202204281618850.png" alt="image-20220428161815769" style="zoom:67%;"><h3 id="公网域名" tabindex="-1">公网域名 <a class="header-anchor" href="#公网域名" aria-label="Permalink to &quot;公网域名&quot;">​</a></h3><blockquote><p>阿里云域名购买：<a href="https://wanwang.aliyun.com/domain/searchresult/#/?keyword=renshuo&amp;suffix=online" target="_blank" rel="noreferrer">https://wanwang.aliyun.com/domain/searchresult/#/?keyword=renshuo&amp;suffix=online</a></p><p>域名<a href="https://wanwang.aliyun.com/domain/1yuan?spm=5176.20907348.J_6123355440.3.2b80538aYwOvhz" target="_blank" rel="noreferrer">https://wanwang.aliyun.com/domain/1yuan?spm=5176.20907348.J_6123355440.3.2b80538aYwOvhz</a></p><p>注意挑选域名，有很多首年免费的</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801172015850.png" alt="image-20230801172015850" style="zoom:80%;"><blockquote><p>个人信息模版：要审核成功才能购买，点击上面自动跳转到信息填写页，填写完成就是下面</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801172123115.png" alt="image-20230801172123115" style="zoom:67%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801195645143.png" alt="image-20230801195645143" style="zoom:80%;"><blockquote><p>注意：配置完成要重启一下IP，然后等一会再测试，还需要配置下面服务器的安全组才行</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801201826201.png" alt="image-20230801201826201" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801201715903.png" alt="image-20230801201715903" style="zoom:80%;"><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">ping www</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">renshuo</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">xyz</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801201617110.png" alt="image-20230801201617110" style="zoom:80%;"><h2 id="买服务器" tabindex="-1">买服务器 <a class="header-anchor" href="#买服务器" aria-label="Permalink to &quot;买服务器&quot;">​</a></h2><blockquote><p>因为个人的工作经历，选择了阿里云服务器，我们直接买个云服务器 ECS，所谓 ECS 服务器，直接引用官方的介绍：云服务器ECS（Elastic Compute Service）是一种简单高效、处理能力可弹性伸缩的计算服务。帮助您构建更稳定、安全的应用，提升运维效率，降低IT成本，使您更专注于核心业务创新。</p></blockquote><blockquote><p>为了简单起见，<strong>直接一键购买</strong></p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202210022154693.png" alt="image-20221002215454567" style="zoom:67%;"><blockquote><p>地域、镜像、网络类型等我直接选择了默认。考虑到一开始也没有什么人访问，网站完全是静态的，即便买了，以后还可以升降配置，<strong>实例规格</strong>我选择了 <code>1 vCPU 1 GiB</code> 。</p></blockquote><h3 id="公网带宽" tabindex="-1">公网带宽 <a class="header-anchor" href="#公网带宽" aria-label="Permalink to &quot;公网带宽&quot;">​</a></h3><h4 id="付费方式" tabindex="-1">付费方式 <a class="header-anchor" href="#付费方式" aria-label="Permalink to &quot;付费方式&quot;">​</a></h4><p>在<strong>公网带宽</strong>这里，有两种付费方式，一种是按固定带宽，一种是按使用流量。</p><blockquote><p>所谓按固定带宽，先付费后使用，如果用户选择 10M 带宽，阿里云就会划分 10M 独享带宽给用户，官方建议适用于业务场景对于网络带宽要求比较稳定的客户，也就是说你的页面流量比较稳定，选择固定带宽会更合适一些。</p></blockquote><blockquote><p>而所谓按使用流量，先使用后付费，根据具体使用的流量计费，每小时扣费，官方建议适用于业务场景对网络带宽需求变化较大的场景，如平时带宽使用较低但间歇性的出现网络访问高峰的场景。</p></blockquote><h4 id="带宽选择" tabindex="-1">带宽选择 <a class="header-anchor" href="#带宽选择" aria-label="Permalink to &quot;带宽选择&quot;">​</a></h4><blockquote><p>如果你使用固定带宽模式，那么选择多少带宽是合适的呢，我们不妨大致的算一下：</p></blockquote><blockquote><p>所谓网络带宽是指在单位时间（一般指的是1秒钟）内能传输的数据量。网络和高速公路类似，带宽越大，就类似高速公路的车道越多，其通行能力越强，简单的来说，带宽越大，网站的访问速度越快。</p></blockquote><blockquote><p>而 <code>1M</code> 带宽对应的下载峰值就是 <code>128KB/S</code>，这是因为云厂商提供的云服务器带宽的单位是 <code>bit</code>（比特），我们通常说的 <code>1M</code> 完整写法其实是 <code>1Mb/s</code>，注意这其中的 <code>b</code> 是小写的。而用户下载速度使用的单位是 <code>Byte</code>（字节），<code>1Byte</code>（字节）= <code>8bit</code>（比特），所以 <code>1Mb = 1/8MB = 0.125MB</code>，我们知道 <code>1MB = 1024KB</code>，所以 <code>1Mb = 0.125MB = 128KB</code>，当然你也可以这样换算：</p></blockquote><blockquote><p>1Mbit/s = 1024kbit/s = 1024/8(KByte/s) = 128(KByte/s)</p></blockquote><blockquote><p>总之带宽是下载速度的 8 倍，1M 带宽对应下载速度是 128KB/s，2M 对应 256KB/s，4M 对应 512KB/s，依此类推。那我们的页面的资源大小是多少呢？我们可以在浏览器的 <code>NetWorks</code> 选项中查看页面的总资源大小，这里以我搭建的 <strong>TypeScript 学习站点</strong>[5]为例，打开开发工具查看：</p></blockquote><p><img src="https://mmbiz.qpic.cn/mmbiz_png/UfCRfwFgbJ2eFI1QicvDYY3Yo9B8KtY67DRhciadhia9AJEdKhrssroKbSk9oX9otLichGTmeDn4MY06XKjb8qp5bw/640?wx_fmt=png&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p><blockquote><p>我们可以看到已传输的资源大小为 <code>443kB</code>，所选资源大小为 <code>852kB</code>，之所以会有差别，是因为服务器和浏览器传输的数据是可以被压缩的，就比如 gzip 压缩。</p></blockquote><blockquote><p>当客户端和服务端握手的时候，客户端会告诉服务端是否支持压缩，如果服务端开启了压缩，且客户端支持压缩，便会将压缩后的数据传输过去，客户端再进行解压操作，我们可以在 <code>headers</code> 的 <code>content-encoding</code> 中查看压缩方式：</p></blockquote><p><img src="https://mmbiz.qpic.cn/mmbiz_png/UfCRfwFgbJ2eFI1QicvDYY3Yo9B8KtY67ZIA5X3ZVDlGNLlxyTzOib8KZtsOT6q19MN6qI0jUqibzl8V5A2IAxHfQ/640?wx_fmt=png&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p><blockquote><p>那这里实际传输的大小，就是 <code>443kB</code>，如果我们希望用户能够在 1s 之内就打开我们的网站，我们至少需要 <code>443 / 128 = 3.46</code> M 的带宽，当然这样算，非常的粗糙，用户的带宽、CDN 优化等等都没有考虑进去，所以就是这么随便一算，如果优化做的好，即便只有 1M 带宽，也可以带来不错的效果。</p></blockquote><p>如果我们 4M 的固定带宽买 1 个月，这里给的价格是 155.60 元。</p><blockquote><p>但如果我们使用按流量付费，如果 4M 每秒都跑满（每秒都有很多人访问），总共的流量为：4 * 128KB/S * 86400 = 11059200KB = 42.1875GB，按照￥0.800/GB 的价格算，每天是大概是 33 元，一个月大概是 1000 元左右，这也就是为什么，如果你的流量比较稳定，会建议使用固定带宽。</p></blockquote><blockquote><p>那我们再算一个例子，如果我的网站每天有 1000 PV，假设他们打开了首页就撤了，大概产生的流量为 <code>1000 * 443KB = 0.42GB</code>，每天的费用为 3 毛钱，一个月大概是 9 元。考虑刚开始也没有什么流量，这里我选择了按使用流量计费，并设置了最大网络带宽为 25M，之所以设置最大网络带宽，是考虑到突然爆发的流量产生较高的费用，我们指定容许的最大网络带宽进行一点限制。</p></blockquote><h3 id="重置实例密码" tabindex="-1">重置实例密码 <a class="header-anchor" href="#重置实例密码" aria-label="Permalink to &quot;重置实例密码&quot;">​</a></h3><blockquote><p>如果是一键购买，我们应该会看到这样一则提示：</p></blockquote><p><img src="https://mmbiz.qpic.cn/mmbiz_png/UfCRfwFgbJ2eFI1QicvDYY3Yo9B8KtY67HAPdX4IGibZgPyISt1YDE6VmBp6jZCgpU2YnSXanO99qoT9fiaA0yl0Q/640?wx_fmt=png&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p><blockquote><p>在购买后，我们根据这个页面**<a href="https://help.aliyun.com/document_detail/25439.html**%5B6%5D" target="_blank" rel="noreferrer">https://help.aliyun.com/document_detail/25439.html**[6]</a> 的操作示例，重置一下密码，否则我们无法登陆服务器。</p></blockquote><h3 id="配置安全组" tabindex="-1">配置安全组 <a class="header-anchor" href="#配置安全组" aria-label="Permalink to &quot;配置安全组&quot;">​</a></h3><blockquote><p>我们知道，当我们使用 HTTP 协议访问网站的时候，默认监听的是 80 端口，但阿里云服务器默认关闭 80 端口，为了能支持 HTTP 访问，我们登陆云服务器 ECS 管理后台，选择安全组，再点击第一个安全组：</p></blockquote><p><img src="https://mmbiz.qpic.cn/mmbiz_png/UfCRfwFgbJ2eFI1QicvDYY3Yo9B8KtY671Dx1PBA452vTQRIe9xApQ2wPySApHWrRH5CzDCbrQzYY86qTyGxEGQ/640?wx_fmt=png&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p><blockquote><p>点击<code>手动添加</code>，添加 80 、443、23端口，添加完的效果如下：</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801201023254.png" alt="image-20230801201023254" style="zoom:80%;"><h2 id="应用安装" tabindex="-1">应用安装 <a class="header-anchor" href="#应用安装" aria-label="Permalink to &quot;应用安装&quot;">​</a></h2><blockquote><p>自动安装官网：<a href="https://oneinstack.com/" target="_blank" rel="noreferrer">https://oneinstack.com/</a></p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801202136693.png" alt="image-20230801202136693" style="zoom:80%;"><blockquote><p>安装命令：得安装好久</p></blockquote><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">wget -c http://mirrors.linuxeye.com/oneinstack-full.tar.gz &amp;&amp; tar xzf oneinstack-full.tar.gz &amp;&amp; ./oneinstack/install.sh --nginx_option 1 --php_option 9 --phpcache_option 1 --db_option 2 --dbinstallmethod 1 --dbrootpwd 315217 --reboot</span></span></code></pre></div><blockquote><p>安装完成后文件夹目录</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801222000129.png" alt="image-20230801222000129" style="zoom:80%;"><h2 id="ssl证书" tabindex="-1">SSL证书 <a class="header-anchor" href="#ssl证书" aria-label="Permalink to &quot;SSL证书&quot;">​</a></h2><blockquote><p>证书官网：<a href="https://yundun.console.aliyun.com/?spm=0.2020520163.products-recent.dcas.4d41799d1wawKx&amp;p=cas#/certExtend/free/cn-hangzhou" target="_blank" rel="noreferrer">SSL证书</a></p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801223245202.png" alt="image-20230801223245202" style="zoom:67%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801223428141.png" alt="image-20230801223428141" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801223546394.png" alt="image-20230801223546394" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801223606064.png" alt="image-20230801223606064" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801223645591.png" alt="image-20230801223645591" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801223738722.png" alt="image-20230801223738722" style="zoom:80%;"><blockquote><p>将下面的两个文件复制到nginx的conf文件中</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.11.17/image-20230801223949123.png" alt="image-20230801223949123" style="zoom:80%;"><h2 id="ca签名" tabindex="-1">CA签名 <a class="header-anchor" href="#ca签名" aria-label="Permalink to &quot;CA签名&quot;">​</a></h2><blockquote><p>下载地址：<a href="https://www.hohnstaedt.de/xca/index.php/download" target="_blank" rel="noreferrer">https://www.hohnstaedt.de/xca/index.php/download</a></p></blockquote><h1 id="可视化配置" tabindex="-1">可视化配置 <a class="header-anchor" href="#可视化配置" aria-label="Permalink to &quot;可视化配置&quot;">​</a></h1><p><a href="https://mp.weixin.qq.com/s?__biz=MzU1Nzg4NjgyMw==&amp;mid=2247499315&amp;idx=1&amp;sn=4ae8c55c415ab2ed844c8e2a706bc459&amp;chksm=fc2c423bcb5bcb2da2af884d6d0a0759e1a9353851c72cd6dabd49f8501f717710eb854e8a72&amp;mpshare=1&amp;scene=23&amp;srcid=0723UxY0M9AxOffTEmjC2zB0&amp;sharer_sharetime=1658554673231&amp;sharer_shareid=29b8a04db1dbd975e3bf4e9f47e7ac67#rd" target="_blank" rel="noreferrer">还在手撸 Nginx 配置？试试这款可视化配置工具吧，真心强大！ (qq.com)</a></p><p><a href="https://www.digitalocean.com/community/tools/nginx?domains.0.routing.index=index.html&amp;domains.0.routing.fallbackHtml=true&amp;global.app.lang=zhCN" target="_blank" rel="noreferrer">NGINXConfig | DigitalOcean</a>：能自动生成nginx配置文件内容</p><h1 id="前后端项目部署-⭐" tabindex="-1">前后端项目部署 ⭐ <a class="header-anchor" href="#前后端项目部署-⭐" aria-label="Permalink to &quot;前后端项目部署 ⭐&quot;">​</a></h1><h2 id="部署前端项目" tabindex="-1">部署前端项目 <a class="header-anchor" href="#部署前端项目" aria-label="Permalink to &quot;部署前端项目&quot;">​</a></h2><h3 id="背景" tabindex="-1">背景 <a class="header-anchor" href="#背景" aria-label="Permalink to &quot;背景&quot;">​</a></h3><ul><li><p><strong>Nginx</strong>可以作为静态web服务器来部署静态资源**。静态资源**指在服务端真实存在并且能够直接展示的一些文件，比如常见的html页面、css文件、js文件、图片、视频等资源。</p></li><li><p>相对于Tomcat，Nginx处理静态资源的能力更加高效，所以在生产环境下，一般都会将静态资源部署到Nginx中。</p></li><li><p>将静态资源部署到Nginx非常简单，只需要将文件复制到Nginx安装目录下的html目录中即可。</p></li></ul><h3 id="实战" tabindex="-1">实战 <a class="header-anchor" href="#实战" aria-label="Permalink to &quot;实战&quot;">​</a></h3><p><a href="https://www.runoob.com/vue3/vue3-build.html" target="_blank" rel="noreferrer">Vue3 项目打包 | 菜鸟教程 (runoob.com)</a></p><p>在vue.config.js中加入如下代码即可，最下面那个</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211843273.png" alt="image-20220621184341109" style="zoom:67%;"><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">module</span><span style="color:#F78C6C;">.exports</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">  publicPath</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">./</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>再进行打包，显示图片成功，生成dist目录</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">npm run build</span></span></code></pre></div><p>将dist目录拷贝到nginx和html同级目录下</p><p>编写配置文件：就是复制一份server改一改</p><p><a href="https://blog.csdn.net/m0_65376942/article/details/123922663" target="_blank" rel="noreferrer">(42条消息) vue项目用nginx部署完成界面刷新404_元气满满ya~的博客-CSDN博客_nginx部署vue项目刷新404</a></p><p>方式一</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   listen       </span><span style="color:#F78C6C;">8089</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   server_name  localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">       root   dist</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">       index  index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">htm</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">       </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 这条命令是避免刷新</span><span style="color:#F78C6C;">404</span><span style="color:#A6ACCD;">的</span></span>
<span class="line"><span style="color:#A6ACCD;">       try_files $uri $uri</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">   error_page   </span><span style="color:#F78C6C;">500</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">502</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">503</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">504</span><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">50x</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">       location </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">50x</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">       root   html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><p>方式二</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">   listen       </span><span style="color:#F78C6C;">8089</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   server_name  localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">       root   dist</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">       index  index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">htm</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">       </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 这条命令是避免刷新</span><span style="color:#F78C6C;">404</span><span style="color:#A6ACCD;">的</span></span>
<span class="line"><span style="color:#A6ACCD;">       try_files $uri $uri</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">index</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">#</span><span style="color:#A6ACCD;"> 拦截静态资源请求</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">缓存，压缩</span></span>
<span class="line"><span style="color:#A6ACCD;">   location </span><span style="color:#89DDFF;">~/.*\.</span><span style="color:#A6ACCD;">(png</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">jpg</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">gif</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">js</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">css)</span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">         root dist</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">         expires </span><span style="color:#F78C6C;">30d</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">         add_header Pragma public</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">         add_header Cache</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">Control </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">public</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">         gzip on</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">   error_page   </span><span style="color:#F78C6C;">500</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">502</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">503</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">504</span><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">50x</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">       location </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">/</span><span style="color:#F78C6C;">50x</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">html </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">       root   html</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">systemctl restart nginx</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">service</span></span></code></pre></div><p>进行访问：成功显示页面</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">http</span><span style="color:#89DDFF;">://</span><span style="color:#F78C6C;">192.168.220.130</span><span style="color:#A6ACCD;">:</span><span style="color:#F78C6C;">8089</span><span style="color:#89DDFF;">/</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206231412018.png" alt="image-20220623141210888" style="zoom:67%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.11/202206211857770.png" alt="image-20220621185717577" style="zoom:50%;"><h2 id="部署后台项目" tabindex="-1">部署后台项目 <a class="header-anchor" href="#部署后台项目" aria-label="Permalink to &quot;部署后台项目&quot;">​</a></h2><p>注意点：mysql地址，静态资源保存位置</p><p>首先要打包成jar包</p><p>然后上传到centos7里，执行进行启动项目</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">java </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">jar reggie_take_out</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">1.0</span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">SNAPSHOT</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">jar</span></span></code></pre></div><p>然后修改nginx.conf</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">server </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">  listen       </span><span style="color:#F78C6C;">8088</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">  server_name  localhost</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">  location </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">      proxy_pass :</span><span style="color:#F78C6C;">8080</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span></code></pre></div><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">systemctl restart nginx</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">service</span></span></code></pre></div><p>进行访问</p><p><a href="http://192.168.220.130:8088/backend/page/login/login.html" target="_blank" rel="noreferrer">http://192.168.220.130:8088/backend/page/login/login.html</a></p></div></div></main><footer class="VPDocFooter" data-v-6b87e69f data-v-37656e44><!--[--><!--]--><!----><nav class="prev-next" data-v-37656e44><div class="pager" data-v-37656e44><a class="pager-link prev" href="/notebook/Linux/%E8%BD%AF%E4%BB%B6%E9%83%A8%E7%BD%B2.html" data-v-37656e44><span class="desc" data-v-37656e44>Previous page</span><span class="title" data-v-37656e44>软件部署</span></a></div><div class="pager" data-v-37656e44><a class="pager-link next" href="/notebook/Nginx/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-37656e44><span class="desc" data-v-37656e44>Next page</span><span class="title" data-v-37656e44>进阶篇</span></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
    <script>window.__VP_HASH_MAP__=JSON.parse("{\"2、数据库_mysql_mysql面试_基础.md\":\"40da680a\",\"1、学前端_5、小程序_小程序项目.md\":\"60a1629b\",\"1、学前端_4、node_知识篇.md\":\"a7fb500e\",\"1、学前端_2、js_ts_es6 进阶.md\":\"6d07ba10\",\"1、学前端_3、vue_vue3_vue3进阶.md\":\"7ac622b4\",\"5、运维_jenkins.md\":\"929081f8\",\"1、学前端_2、js_ts_typescript.md\":\"875a4aa4\",\"2、数据库_mysql_mysql核心_设计.md\":\"7faf46d1\",\"2、数据库_mysql_mysql核心_基础.md\":\"d8e97f3e\",\"1、学前端_1、html_css_html基础.md\":\"7584d076\",\"1、学前端_5、专题篇_问题篇.md\":\"e893aaa2\",\"2、数据库_mysql_mysql面试_进阶.md\":\"f934806d\",\"3、springboot_运维_原理.md\":\"f4a39db6\",\"2、数据库_influxdb.md\":\"6e1711e1\",\"3、springboot_新特性.md\":\"cdf3e307\",\"mybatis_mybatisplus_jpa.md\":\"8e41681b\",\"1、学前端_5、小程序_小程序优化.md\":\"a2185198\",\"2、数据库_redis_redis基础.md\":\"856df0e0\",\"linux_实用脚本.md\":\"f2299dd5\",\"4、微服务_必备_分布式基础.md\":\"d49863d5\",\"2、数据库_redis_redis优化.md\":\"e66ae32f\",\"4、微服务_springsecurity_进阶篇.md\":\"235a8e9e\",\"5、运维_chatgpt.md\":\"10db3823\",\"2、数据库_mysql_分库分表.md\":\"e1c8a095\",\"start.md\":\"9bc1ff8d\",\"5、运维_github.md\":\"2ec6c735\",\"java学前端_css.md\":\"f11b47f0\",\"1、学前端_5、专题篇_知识篇.md\":\"a463ed8d\",\"linux_软件部署.md\":\"d6722925\",\"2、数据库_neo4j.md\":\"97ad22ac\",\"team.md\":\"ce467a6a\",\"nginx_实战篇.md\":\"7785486e\",\"index.md\":\"8c3ec167\",\"计算机基础_计算机网络_网络基础.md\":\"7a54a85d\",\"1、学前端_4、node_进阶篇.md\":\"60f6db69\",\"java_java集合.md\":\"a049b313\",\"1、学前端_3、vue_vue3_vue3高级.md\":\"614d1516\",\"1、学前端_5、小程序_微信小程序.md\":\"9a4be771\",\"5、运维_netty.md\":\"12ca0278\",\"2、数据库_mysql_mysql核心_运维.md\":\"83f97c16\",\"idea_vs code.md\":\"afdcb593\",\"java学前端_vue3_组件.md\":\"1086884e\",\"idea_chrome.md\":\"4a32afbc\",\"云原生_k8s.md\":\"db58e65a\",\"2、数据库_mysql_mysql核心_进阶.md\":\"61d16dff\",\"ssm_springbatch.md\":\"f799ab4a\",\"三高_分布式.md\":\"db1b8a1b\",\"2、数据库_elasticsearch_1、es基础.md\":\"04d17448\",\"linux_linux基础.md\":\"4b0bf394\",\"idea_idea插件.md\":\"fa86e45a\",\"可视化 _ 监控_可视化大屏.md\":\"004553bd\",\"2、数据库_mongodb_整合.md\":\"3c47d7f4\",\"4、微服务_springsecurity_基础篇.md\":\"534a3401\",\"4、微服务_进阶.md\":\"69095c58\",\"计算机基础_计算机基础_操作系统.md\":\"0f75d113\",\"可视化 _ 监控_zabbix.md\":\"71f2270e\",\"nginx_基础篇.md\":\"c7d8bb50\",\"1、学前端_4、node_项目实战.md\":\"bc5065b8\",\"2、数据库_redis_redis原理.md\":\"5cedf685\",\"可视化 _ 监控_监控基础.md\":\"ac56ce4d\",\"三高_高并发.md\":\"ea9ffc99\",\"2、数据库_redis_redis高级.md\":\"1d5872f6\",\"1、学前端_4、node_基础篇.md\":\"581cc13a\",\"2、数据库_mongodb_基础.md\":\"fb7a0a29\",\"idea_idea基础.md\":\"6f2f9638\",\"4、微服务_必备_sentinel.md\":\"2edfbf6c\",\"2、数据库_elasticsearch_3、es高级.md\":\"ef146606\",\"1、学前端_3、vue_vue3_vue3新语法.md\":\"8afd5409\",\"消息中间件_canal.md\":\"3949163c\",\"ssm_maven.md\":\"2c5e12ed\",\"4、微服务_springsecurity_高级篇.md\":\"882d3ff3\",\"linux_linux进阶.md\":\"188ef7b4\",\"计算机基础_设计模式_uml.md\":\"634ba256\",\"计算机基础_算法_leetcode.md\":\"77162fb9\",\"项目实战_小兔鲜_进阶篇1.md\":\"17c52c81\",\"1、学前端_2、js_ts_es6 基础.md\":\"fda3f18b\",\"项目实战_小兔鲜_进阶篇2.md\":\"a0f23006\",\"软件测试_测试基础.md\":\"8c1060cd\",\"2、数据库_redis_本地缓存.md\":\"00617fe6\",\"nginx_面试篇.md\":\"e3fb373a\",\"mybatis_mybatisplus_mybatis.md\":\"9239e0ad\",\"linux_shell.md\":\"ae53d83b\",\"2、数据库_mysql_mysql核心_优化.md\":\"36230425\",\"项目实战_项目推荐.md\":\"f9d97630\",\"mybatis_mybatisplus_mybatisplus.md\":\"0030fd35\",\"项目实战_百度地图_进阶篇.md\":\"c8b93267\",\"三高_高可用.md\":\"323840c5\",\"java_java新特性.md\":\"22abf56d\",\"软件测试_压力测试.md\":\"9ab44440\",\"java学前端_html_js.md\":\"e0fcd240\",\"2、数据库_redis_redis实战.md\":\"d6daeeab\",\"nginx_进阶篇.md\":\"e6b63195\",\"三高_秒杀.md\":\"3878bb64\",\"5、运维_git.md\":\"0264925c\",\"java_java进阶.md\":\"e79cb5b4\",\"并发 _ 多线程_基础篇.md\":\"7adbfac5\",\"项目实战_百度地图_基础篇.md\":\"8afa5954\",\"java学前端_react.md\":\"3ec827dd\",\"1、学前端_1、html_css_css基础.md\":\"01b56712\",\"项目实战_小兔鲜_基础篇.md\":\"646f5df5\",\"1、学前端_2、js_ts_js 基础.md\":\"cb13e36f\",\"可视化 _ 监控_监控进阶.md\":\"0cdbc292\",\"计算机基础_设计模式_基础篇.md\":\"51617287\",\"计算机基础_数据结构_基础篇.md\":\"b2bfd8d4\",\"项目实战_苍穹外卖_进阶篇.md\":\"48415e41\",\"ssm_spring.md\":\"ab514659\",\"消息中间件_rabbitmq.md\":\"45b1eb28\",\"1、学前端_1、html_css_网页进阶.md\":\"db998248\",\"消息中间件_kafka.md\":\"b747dabf\",\"云原生_docker.md\":\"983c7ba7\",\"4、微服务_必备_分布式锁.md\":\"5af1cf8d\",\"消息中间件_rocketmq.md\":\"d441da85\",\"项目实战_黑马头条_基础篇.md\":\"b05af3a6\",\"ssm_springmvc.md\":\"81b9714f\",\"项目实战_支付.md\":\"1d7407dd\",\"项目实战_黑马头条_进阶篇2.md\":\"bff0015b\",\"项目实战_黑马头条_进阶篇.md\":\"19f18388\",\"java学前端_vue2_组件.md\":\"58c6b1df\",\"3、springboot_基础篇.md\":\"529c66f4\",\"3、springboot_应用篇.md\":\"8b92aa61\",\"项目实战_黑马头条_高级篇.md\":\"227c08c1\",\"1、学前端_5、小程序_uniapp.md\":\"71a282b4\",\"项目实战_云尚办公_基础篇.md\":\"1fe188ba\",\"并发 _ 多线程_并发完善.md\":\"26619c46\",\"1、学前端_2、js_ts_js 进阶.md\":\"657dfb8f\",\"java_java高级.md\":\"23782d1a\",\"java_java基础.md\":\"86d67c77\"}");window.__VP_SITE_DATA__=JSON.parse("{\"lang\":\"en-US\",\"dir\":\"ltr\",\"title\":\"VitePress\",\"description\":\"A VitePress site\",\"base\":\"/notebook/\",\"head\":[],\"appearance\":true,\"themeConfig\":{\"algolia\":{\"appId\":\"DW7O63I9IR\",\"apiKey\":\"f8ed758cdb288a8b06542bc35923c1a1\",\"indexName\":\"notebook\"},\"sidebar\":[{\"text\":\"Java\",\"collapsed\":true,\"items\":[{\"text\":\"Java基础\",\"link\":\"/Java/Java基础\"},{\"text\":\"Java新特性\",\"link\":\"/Java/Java新特性\"},{\"text\":\"Java进阶\",\"link\":\"/Java/Java进阶\"},{\"text\":\"Java集合\",\"link\":\"/Java/Java集合\"},{\"text\":\"Java高级\",\"link\":\"/Java/Java高级\"}]},{\"text\":\"Linux\",\"collapsed\":true,\"items\":[{\"text\":\"Linux基础\",\"link\":\"/Linux/Linux基础\"},{\"text\":\"Linux新特性\",\"link\":\"/Linux/Linux进阶\"},{\"text\":\"Shell脚本\",\"link\":\"/Linux/Shell\"},{\"text\":\"实用脚本\",\"link\":\"/Linux/实用脚本\"},{\"text\":\"软件部署\",\"link\":\"/Linux/软件部署\"}]},{\"text\":\"Nginx\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/Nginx/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/Nginx/进阶篇\"},{\"text\":\"实战篇\",\"link\":\"/Nginx/实战篇\"},{\"text\":\"面试篇\",\"link\":\"/Nginx/面试篇\"}]},{\"text\":\"SSM\",\"collapsed\":true,\"items\":[{\"text\":\"Maven\",\"link\":\"/SSM/Maven\"},{\"text\":\"Spring\",\"link\":\"/SSM/Spring\"},{\"text\":\"SpringMVC\",\"link\":\"/SSM/SpringMVC\"},{\"text\":\"SpringBatch\",\"link\":\"/SSM/SpringBatch\"}]},{\"text\":\"SpringBoot\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/3、SpringBoot/基础篇\"},{\"text\":\"应用篇\",\"link\":\"/3、SpringBoot/应用篇\"},{\"text\":\"新特性\",\"link\":\"/3、SpringBoot/新特性\"},{\"text\":\"运维&原理\",\"link\":\"/3、SpringBoot/运维&原理\"}]},{\"text\":\"SpringCloud\",\"collapsed\":true,\"items\":[{\"text\":\"SpringCloud\",\"link\":\"/4、微服务/进阶\"},{\"text\":\"Sentinel\",\"link\":\"/4、微服务/必备/Sentinel\"}]},{\"text\":\"SpringSecurity\",\"collapsed\":true,\"items\":[{\"text\":\"SpringSecurity基础篇\",\"link\":\"/4、微服务/SpringSecurity/基础篇\"},{\"text\":\"SpringSecurity进阶篇\",\"link\":\"/4、微服务/SpringSecurity/进阶篇\"},{\"text\":\"SpringSecurity高级篇\",\"link\":\"/4、微服务/SpringSecurity/高级篇\"}]},{\"text\":\"Mybatis & MybatisPlus\",\"collapsed\":true,\"items\":[{\"text\":\"Mybatis\",\"link\":\"/Mybatis&MybatisPlus/Mybatis\"},{\"text\":\"MybatisPlus\",\"link\":\"/Mybatis&MybatisPlus/MybatisPlus\"},{\"text\":\"JPA\",\"link\":\"/Mybatis&MybatisPlus/JPA\"}]},{\"text\":\"Git & ChatGPT\",\"collapsed\":true,\"items\":[{\"text\":\"Git\",\"link\":\"/5、运维/Git\"},{\"text\":\"Github\",\"link\":\"/5、运维/Github\"},{\"text\":\"ChatGPT\",\"link\":\"/5、运维/ChatGPT\"},{\"text\":\"Jenkins\",\"link\":\"/5、运维/Jenkins\"},{\"text\":\"Netty\",\"link\":\"/5、运维/Netty\"}]},{\"text\":\"数据库\",\"collapsed\":true,\"items\":[{\"text\":\"MySQL\",\"collapsed\":true,\"items\":[{\"text\":\"MySQL基础\",\"link\":\"/2、数据库/MySQL/MySQL核心/基础\"},{\"text\":\"MySQL进阶\",\"link\":\"/2、数据库/MySQL/MySQL核心/进阶\"},{\"text\":\"MySQL优化\",\"link\":\"/2、数据库/MySQL/MySQL核心/优化\"},{\"text\":\"MySQL设计\",\"link\":\"/2、数据库/MySQL/MySQL核心/设计\"},{\"text\":\"MySQL运维\",\"link\":\"/2、数据库/MySQL/MySQL核心/运维\"},{\"text\":\"分库分表\",\"link\":\"/2、数据库/MySQL/分库分表\"}]},{\"text\":\"Redis\",\"collapsed\":true,\"items\":[{\"text\":\"Redis基础\",\"link\":\"/2、数据库/Redis/Redis基础\"},{\"text\":\"Redis优化\",\"link\":\"/2、数据库/Redis/Redis优化\"},{\"text\":\"Redis原理\",\"link\":\"/2、数据库/Redis/Redis原理\"},{\"text\":\"Redis高级\",\"link\":\"/2、数据库/Redis/Redis高级\"},{\"text\":\"Redis实战\",\"link\":\"/2、数据库/Redis/Redis实战\"},{\"text\":\"本地缓存\",\"link\":\"/2、数据库/Redis/本地缓存\"}]},{\"text\":\"MongoDB\",\"collapsed\":true,\"items\":[{\"text\":\"MongoDB基础\",\"link\":\"/2、数据库/MongoDB/基础\"},{\"text\":\"MongoDB进阶\",\"link\":\"/2、数据库/MongoDB/整合\"}]},{\"text\":\"ElasticSearch\",\"collapsed\":true,\"items\":[{\"text\":\"ES基础\",\"link\":\"/2、数据库/ElasticSearch/1、ES基础\"},{\"text\":\"ES高级\",\"link\":\"/2、数据库/ElasticSearch/3、ES高级\"}]},{\"text\":\"InfluxDB\",\"link\":\"/2、数据库/influxdb\"},{\"text\":\"Neo4j\",\"link\":\"/2、数据库/Neo4j\"}]},{\"text\":\"高并发 & 秒杀 & 分布式\",\"collapsed\":true,\"items\":[{\"text\":\"分布式理论\",\"link\":\"/三高/分布式\"},{\"text\":\"分布式锁\",\"link\":\"/4、微服务/必备/分布式锁\"},{\"text\":\"秒杀\",\"link\":\"/三高/秒杀\"},{\"text\":\"高可用\",\"link\":\"/三高/高可用\"},{\"text\":\"高并发\",\"link\":\"/三高/高并发\"}]},{\"text\":\"云原生\",\"collapsed\":true,\"items\":[{\"text\":\"Docker\",\"link\":\"/云原生/Docker\"},{\"text\":\"K8S\",\"link\":\"/云原生/K8S\"}]},{\"text\":\"可视化 & 监控\",\"collapsed\":true,\"items\":[{\"text\":\"监控基础\",\"link\":\"/可视化 & 监控/监控基础\"},{\"text\":\"监控进阶\",\"link\":\"/可视化 & 监控/监控进阶\"},{\"text\":\"可视化大屏\",\"link\":\"/可视化 & 监控/可视化大屏\"},{\"text\":\"Zabbix\",\"link\":\"/可视化 & 监控/Zabbix\"}]},{\"text\":\"学前端\",\"collapsed\":true,\"items\":[{\"text\":\"HTML+CSS\",\"collapsed\":true,\"items\":[{\"text\":\"HTML基础\",\"link\":\"/1、学前端/1、HTML+CSS/HTML基础\"},{\"text\":\"CSS基础\",\"link\":\"/1、学前端/1、HTML+CSS/CSS基础\"},{\"text\":\"网页进阶\",\"link\":\"/1、学前端/1、HTML+CSS/网页进阶\"}]},{\"text\":\"JS+TS\",\"collapsed\":true,\"items\":[{\"text\":\"JS基础\",\"link\":\"/1、学前端/2、JS+TS/JS 基础\"},{\"text\":\"JS进阶\",\"link\":\"/1、学前端/2、JS+TS/JS 进阶\"},{\"text\":\"ES6基础\",\"link\":\"/1、学前端/2、JS+TS/ES6 基础\"},{\"text\":\"ES6进阶\",\"link\":\"/1、学前端/2、JS+TS/ES6 进阶\"},{\"text\":\"TS基础\",\"link\":\"/1、学前端/2、JS+TS/TypeScript\"}]},{\"text\":\"NodeJS\",\"collapsed\":true,\"items\":[{\"text\":\"Node基础\",\"link\":\"/1、学前端/4、Node/基础篇\"},{\"text\":\"Node进阶\",\"link\":\"/1、学前端/4、Node/进阶篇\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/4、Node/项目实战\"}]},{\"text\":\"Vue\",\"collapsed\":true,\"items\":[{\"text\":\"Vue3进阶\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3进阶\"},{\"text\":\"Vue3高级\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3高级\"},{\"text\":\"Vue3新语法\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3新语法\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/3、Vue/Vue2/Vue2项目\"}]},{\"text\":\"小程序\",\"collapsed\":true,\"items\":[{\"text\":\"小程序基础\",\"link\":\"/1、学前端/5、小程序/微信小程序\"},{\"text\":\"小程序优化\",\"link\":\"/1、学前端/5、小程序/小程序优化\"},{\"text\":\"uniapp\",\"link\":\"/1、学前端/5、小程序/uniapp\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/5、小程序/小程序项目\"}]}]},{\"text\":\"计算机基础\",\"collapsed\":true,\"items\":[{\"text\":\"数据结构\",\"link\":\"/计算机基础/数据结构/基础篇\"},{\"text\":\"操作系统\",\"link\":\"/计算机基础/计算机基础/操作系统\"},{\"text\":\"设计模式\",\"link\":\"/计算机基础/设计模式/基础篇\"},{\"text\":\"计算机网络\",\"link\":\"/计算机基础/计算机网络/网络基础\"},{\"text\":\"UML\",\"link\":\"/计算机基础/设计模式/UML\"},{\"text\":\"LeetCode\",\"link\":\"/计算机基础/算法/LeetCode\"}]},{\"text\":\"项目实战\",\"collapsed\":true,\"items\":[{\"text\":\"云尚办公\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/云尚办公/基础篇\"}]},{\"text\":\"小兔鲜\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/小兔鲜/基础篇\"},{\"text\":\"进阶篇1\",\"link\":\"/项目实战/小兔鲜/进阶篇1\"},{\"text\":\"进阶篇2\",\"link\":\"/项目实战/小兔鲜/进阶篇2\"}]},{\"text\":\"地图\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/百度地图/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/项目实战/百度地图/进阶篇\"}]},{\"text\":\"苍穹外卖\",\"collapsed\":true,\"items\":[{\"text\":\"进阶篇\",\"link\":\"/项目实战/苍穹外卖/进阶篇\"}]},{\"text\":\"黑马头条\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/黑马头条/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/项目实战/黑马头条/进阶篇\"},{\"text\":\"进阶篇2\",\"link\":\"/项目实战/黑马头条/进阶篇2\"},{\"text\":\"高级篇\",\"link\":\"/项目实战/黑马头条/高级篇\"}]},{\"text\":\"支付\",\"link\":\"/项目实战/支付\"},{\"text\":\"项目推荐\",\"link\":\"/项目实战/项目推荐\"}]},{\"text\":\"团队成员\",\"link\":\"/team\"}],\"siteTitle\":\"任硕的文档\",\"logo\":\"/Vue.png\",\"nav\":[{\"text\":\"Java学前端\",\"items\":[{\"items\":[{\"text\":\"HTML+JS\",\"link\":\"/Java学前端/HTML+JS\"},{\"text\":\"CSS\",\"link\":\"/Java学前端/CSS\"},{\"text\":\"Vue2+组件\",\"link\":\"/Java学前端/Vue2+组件\"},{\"text\":\"Vue3+组件\",\"link\":\"/Java学前端/Vue3+组件\"},{\"text\":\"React\",\"link\":\"/Java学前端/React\"}]}],\"activeMatch\":\"/Java/\"},{\"text\":\"软件测试\",\"items\":[{\"items\":[{\"text\":\"测试基础\",\"link\":\"/软件测试/测试基础\"},{\"text\":\"压力测试\",\"link\":\"/软件测试/压力测试\"}]}]},{\"text\":\"多线程\",\"items\":[{\"items\":[{\"text\":\"基础篇\",\"link\":\"/并发 & 多线程/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/并发 & 多线程/并发完善\"}]}]},{\"text\":\"开发工具\",\"items\":[{\"items\":[{\"text\":\"Chrome\",\"link\":\"/IDEA/Chrome\"},{\"text\":\"IDEA基础\",\"link\":\"/IDEA/IDEA基础\"},{\"text\":\"IDEA插件\",\"link\":\"/IDEA/IDEA插件\"},{\"text\":\"VS Code\",\"link\":\"/IDEA/VS Code\"}]}]},{\"text\":\"消息中间件\",\"items\":[{\"items\":[{\"text\":\"RabbitMQ\",\"link\":\"/消息中间件/RabbitMQ\"},{\"text\":\"RocketMQ\",\"link\":\"/消息中间件/RocketMQ\"},{\"text\":\"Kafka\",\"link\":\"/消息中间件/Kafka\"},{\"text\":\"Canal\",\"link\":\"/消息中间件/Canal\"}]}]}],\"socialLinks\":[{\"icon\":\"github\",\"link\":\"https://github.com/renshuo123/renshuo123.github.io\"},{\"icon\":\"twitter\",\"link\":\"#\"},{\"icon\":{\"svg\":\"<svg t=\\\"1676028692954\\\" class=\\\"icon\\\" ...</path></svg>\"},\"link\":\"https://github.com/\"}]},\"locales\":{},\"scrollOffset\":90,\"cleanUrls\":false}");</script>
    
  </body>
</html>